Майкл Хартл - Глава 6 - Класс пользователя становится nil и выдает неопределенный метод authenticate для nil: NilClass - PullRequest
0 голосов
/ 03 апреля 2019

Я изучаю Ruby на рельсах, используя http://railstutorial.org/.Проходя 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

...