我之前遇到过这些错误,并添加了
attr_accessible :name, :email, :password, :password_confirmation
排到user.rb
,它似乎解决了我的示例应用程序在开发过程中的一些功能问题,一切都很顺利。然后我决定把它推到heroku,直到我删除了
attr_accessible :name, :email, :password, :password_confirmation
从user.rb
开始。现在rspec失败了。有什么我想念的吗?任何帮助表示赞赏。
' c:\Sites\sample_app>bundle exec rspec spec/
including Capybara::DSL in the global scope is not recommended!
.F.............F......FF..FFF..
Failures:
1) User pages signup with valid information should create a user
Failure/Error: expect { click_button submit }.to change(User, :count).by(1)
count should have been changed by 1, but was changed by 0
# ./spec/requests/user_pages_spec.rb:46:in `block (4 levels) in <top (requi
red)>'
2) User
Failure/Error: it { should be_valid }
expected #<User id: nil, name: nil, email: nil, created_at: nil, updated_
at: nil, password_digest: nil, password: nil> to be valid, but got errors: Passw
ord can't be blank, Password is too short (minimum is 6 characters), Name can't
be blank, Email can't be blank, Email is invalid, Password confirmation can't be
blank
# ./spec/models/user_spec.rb:19:in `block (2 levels) in <top (required)>'
3) User when email address is already taken
Failure/Error: user_with_same_email.email = @user.email.upcase
NoMethodError:
undefined method `upcase' for nil:NilClass
# ./spec/models/user_spec.rb:79:in `block (3 levels) in <top (required)>'
4) User when email format is valid should be valid
Failure/Error: @user.should be_valid
expected #<User id: nil, name: nil, email: "user@foo.COM", created_at: ni
l, updated_at: nil, password_digest: nil, password: nil> to be valid, but got er
rors: Password can't be blank, Password is too short (minimum is 6 characters),
Name can't be blank, Password confirmation can't be blank
# ./spec/models/user_spec.rb:71:in `block (4 levels) in <top (required)>'
# ./spec/models/user_spec.rb:69:in `each'
# ./spec/models/user_spec.rb:69:in `block (3 levels) in <top (required)>'
5) User return value of authenticate method with valid password
Failure/Error: it { should == found_user.authenticate(@user.password) }
NoMethodError:
undefined method `authenticate' for nil:NilClass
# ./spec/models/user_spec.rb:43:in `block (4 levels) in <top (required)>'
6) User return value of authenticate method with invalid password
Failure/Error: let(:user_for_invalid_password) { found_user.authenticate("i
nvalid") }
NoMethodError:
undefined method `authenticate' for nil:NilClass
# ./spec/models/user_spec.rb:47:in `block (4 levels) in <top (required)>'
# ./spec/models/user_spec.rb:49:in `block (4 levels) in <top (required)>'
7) User return value of authenticate method with invalid password
Failure/Error: let(:user_for_invalid_password) { found_user.authenticate("i
nvalid") }
NoMethodError:
undefined method `authenticate' for nil:NilClass
# ./spec/models/user_spec.rb:47:in `block (4 levels) in <top (required)>'
# ./spec/models/user_spec.rb:50:in `block (4 levels) in <top (required)>'
Finished in 0.37081 seconds
31 examples, 7 failures
Failed examples:
rspec ./spec/requests/user_pages_spec.rb:45 # User pages signup with valid infor
mation should create a user
rspec ./spec/models/user_spec.rb:19 # User
rspec ./spec/models/user_spec.rb:83 # User when email address is already taken
rspec ./spec/models/user_spec.rb:67 # User when email format is valid should be
valid
rspec ./spec/models/user_spec.rb:43 # User return value of authenticate method w
ith valid password
rspec ./spec/models/user_spec.rb:49 # User return value of authenticate method w
ith invalid password
rspec ./spec/models/user_spec.rb:50 # User return value of authenticate method w
ith invalid password'
答案 0 :(得分:0)
你的spec
犯了很多错误
2) User Failure/Error: it { should be_valid }
模型无效,因为不保存
3) User when email address is already taken
此故障因为模型未保存
了解valid?
valid?(context = nil)
运行所有指定的验证,如果没有添加错误,则返回true,否则返回false。
my_person = Person.new(params[:person])
my_person.valid?
# => false
my_person = Person.create(params[:person])
my_person.valid?
# => true
my_person.errors.add('login', 'can not be empty') if my_person.login == ''
my_person.valid?
# => false
以及更多只读错误