0
我有以下的測試代碼:失敗RSpec的測試 - 不被創建的對象
require 'spec_helper'
require 'matchers/be_valid_verbose'
describe User do
before do
@user = User.new(first_name: "First", last_name: "Last", email: "[email protected]", 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
末
我不明白爲什麼:它應該{} be_valid是gettig一個完全空的用戶目的。在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: "[email protected]", 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: "[email protected]", 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
你只是毆打我的答案:)另外,在前面的塊創建一個實例變量是要求麻煩,你有'let'! – iain