Michael Hartl-第6章-用户类变为nil并为nil抛出未定义的方法“ authenticate”:NilClass

时间:2019-04-03 07:58:01

标签: ruby-on-rails rspec ruby-on-rails-5 rspec-rails rspec2

我正在使用http://railstutorial.org/学习Ruby on Rails。在阅读本书第6章时。我遇到了我的用户领域变成零。进而导致authenticate方法失败。

仅供参考,与本教程中提到的宝石不同,我使用过rails 5和ruby 2.6是所有最新的宝石。

这是运行测试后的Rspec结果

$bundle exec rspec spec/
including Capybara::DSL in the global scope is not recommended!

Randomized with seed 55216
............F..........FFF..F...

Failures:

  1) User should be valid
     Failure/Error: it { should be_valid }
       expected #<User id: nil, name: nil, email: nil, created_at: nil, updated_at: nil, password_digest: nil> to be valid, but got errors: Password can't be blank
     # ./spec/models/user_spec.rb:27:in `block (2 levels) in <top (required)>'

  2) User return value of authenticate method with valid password should When you call a matcher in an example without a String, like this:

specify { expect(object).to matcher }

or this:

it { is_expected.to matcher }

RSpec expects the matcher to have a #description method. You should either
add a String to the example this matcher is being used in, or give it a
description method. Then you won't have to suffer this lengthy warning again.

     Failure/Error: it { should == found_user.authenticate(@user.password) }

     NoMethodError:
       undefined method `authenticate' for nil:NilClass
     # ./spec/models/user_spec.rb:64:in `block (4 levels) in <top (required)>'

  3) User return value of authenticate method with invalid password 
     Failure/Error: let(:user_for_invalid_password) { found_user.authenticate("invalid") }

     NoMethodError:
       undefined method `authenticate' for nil:NilClass
     # ./spec/models/user_spec.rb:68:in `block (4 levels) in <top (required)>'
     # ./spec/models/user_spec.rb:71:in `block (4 levels) in <top (required)>'

  4) User return value of authenticate method with invalid password should not When you call a matcher in an example without a String, like this:

specify { expect(object).to matcher }

or this:

it { is_expected.to matcher }

RSpec expects the matcher to have a #description method. You should either
add a String to the example this matcher is being used in, or give it a
description method. Then you won't have to suffer this lengthy warning again.

     Failure/Error: let(:user_for_invalid_password) { found_user.authenticate("invalid") }

     NoMethodError:
       undefined method `authenticate' for nil:NilClass
     # ./spec/models/user_spec.rb:68:in `block (4 levels) in <top (required)>'
     # ./spec/models/user_spec.rb:70:in `block (4 levels) in <top (required)>'

  5) User when email format is valid should be valid
     Failure/Error: @user.should be_valid
       expected #<User id: nil, name: nil, email: nil, created_at: nil, updated_at: nil, password_digest: nil> to be valid, but got errors: Password can't be blank
     # ./spec/models/user_spec.rb:96:in `block (4 levels) in <top (required)>'
     # ./spec/models/user_spec.rb:94:in `each'
     # ./spec/models/user_spec.rb:94:in `block (3 levels) in <top (required)>'

Deprecation Warnings:

Using `should` from rspec-expectations' old `:should` syntax without explicitly enabling the syntax is deprecated. Use the new `:expect` syntax or explicitly enable `:should` with `config.expect_with(:rspec) { |c| c.syntax = :should }` instead. Called from /home/user/rails_projects/sample_app/spec/models/user_spec.rb:96:in `block (4 levels) in <top (required)>'.


If you need more of the backtrace for any of these deprecations to
identify where to make the necessary changes, you can configure
`config.raise_errors_for_deprecations!`, and it will turn the
deprecation warnings into errors, giving you the full backtrace.

1 deprecation warning total

Finished in 0.49086 seconds (files took 0.54848 seconds to load)
32 examples, 5 failures

Failed examples:

rspec ./spec/models/user_spec.rb:27 # User should be valid
rspec ./spec/models/user_spec.rb:64 # User return value of authenticate method with valid password should When you call a matcher in an example without a String, like this:

specify { expect(object).to matcher }

or this:

it { is_expected.to matcher }

RSpec expects the matcher to have a #description method. You should either
add a String to the example this matcher is being used in, or give it a
description method. Then you won't have to suffer this lengthy warning again.

rspec ./spec/models/user_spec.rb:71 # User return value of authenticate method with invalid password 
rspec ./spec/models/user_spec.rb:70 # User return value of authenticate method with invalid password should not When you call a matcher in an example without a String, like this:

specify { expect(object).to matcher }

or this:

it { is_expected.to matcher }

RSpec expects the matcher to have a #description method. You should either
add a String to the example this matcher is being used in, or give it a
description method. Then you won't have to suffer this lengthy warning again.

rspec ./spec/models/user_spec.rb:92 # User when email format is valid should be valid

Randomized with seed 55216

项目

sample_app/tree/modelling_user

文件

spec/models/user_spec.rb

app/models/user.rb

Gemfile

0 个答案:

没有答案