我有以下测试代码:
require 'spec_helper'
require 'matchers/be_valid_verbose'
describe User do
before do
@user = User.new(first_name: "First", last_name: "Last", email: "test@test.com", role: "admin",
password: "foobar12", password_confirmation: "foobar12")
end
subject ( @user )
specify { should be_valid_verbose }
describe "return value of authendicate method" do
before { @user.save }
let(:found_user) { User.find_by_email(@user.email) }
describe "with valid password" do
it { should == found_user.authenticate(@user.password) }
end
describe "with invalid password" do
let(:user_for_invalid_password) { found_user.authenticate("invlaid") }
it { should_not == user_for_invalid_password }
specify { user_for_invalid_password.should be_false }
end
end
结尾
我不明白为什么:它 { should be_valid } 是一个完全空的用户对象。当我在 before do 语句中创建用户对象后尝试 find_by_email 时,也会发生同样的情况。这是我的测试输出
$ bundle exec rspec spec/models/user_trial_spec.rb
FF..
Failures:
1) User
Failure/Error: specify { should be_valid_verbose }
expected valid? to return true, got false:
Password digest can't be blank
First name can't be blank
Last name can't be blank
Role is not included in the list
Email can't be blank
Email is invalid
Password can't be blank
Password is too short (minimum is 8 characters)
Password confirmation can't be blank
# ./spec/models/user_trial_spec.rb:25:in `block (2 levels) in <top (required)>'
2) User return value of authendicate method with valid password
Failure/Error: it { should == found_user.authenticate(@user.password) }
expected: #<User id: 10, first_name: "First", last_name: "Last", email: "test@test.com", role: "admin", password_digest: "$2a$10$Z0c6zJNH4yu8IpYfNqEbKOmqEWK.euTFcYuwB/8UW9jk...", created_at: "2012-11-13 21:44:55", updated_at: "2012-11-13 21:44:55", remember_token: nil>
got: #<User id: nil, first_name: nil, last_name: nil, email: nil, role: nil, password_digest: nil, created_at: nil, updated_at: nil, remember_token: nil> (using ==)
Diff:
@@ -1,2 +1,2 @@
-#<User id: 10, first_name: "First", last_name: "Last", email: "test@test.com", role: "admin", password_digest: "$2a$10$Z0c6zJNH4yu8IpYfNqEbKOmqEWK.euTFcYuwB/8UW9jk...", created_at: "2012-11-13 21:44:55", updated_at: "2012-11-13 21:44:55", remember_token: nil>
+#<User id: nil, first_name: nil, last_name: nil, email: nil, role: nil, password_digest: nil, created_at: nil, updated_at: nil, remember_token: nil>
# ./spec/models/user_trial_spec.rb:32:in `block (4 levels) in <top (required)>'
Finished in 2.3 seconds
4 examples, 2 failures
Failed examples:
rspec ./spec/models/user_trial_spec.rb:25 # User
rspec ./spec/models/user_trial_spec.rb:32 # User return value of authendicate method with valid password
Randomized with seed 39450