rspec Error: ActionView :: Template :: Error: undefined method `name'for nil: NilClass

Current status

When you run the spec 「ActionView::Template::Error:undefined method `name' for nil:NilClass」 Error occurs

spec


      it 'The user registration page is displayed' do
        get new_admin_staff_path
        expect(response.status).to eq 200
        expect(response).to render_template :new
      end

factrybot



FactoryBot.define do
  factory :user do
    transient do
      person { Gimei.name }
    end
    association :shop
    sequence(:email) { |i| "admin#{i}@example.com" }
    password { 'abc12345' }
    sequence(:name) { |_i| "#{person.last.kanji} #{person.first.kanji}" }
    shop_id { 1 }
  end
end

Conclusion

In factrybot, the column with ID == 1 was specified, The id was issued every time the spec was passed, and the ID 1 did not exist. So, I didn't know the information to pull out and it was an error.

Therefore, instead of fixing the acquisition of information to ID == 1, It was solved by making the description to get the very first ID. → shop_id { Shop.first.id }

factrybot



FactoryBot.define do
  factory :user do
    transient do
      person { Gimei.name }
    end
    association :shop
    sequence(:email) { |i| "admin#{i}@example.com" }
    password { 'abc12345' }
    sequence(:name) { |_i| "#{person.last.kanji} #{person.first.kanji}" }
    shop_id { Shop.first.id  }
  end
end

Recommended Posts

rspec Error: ActionView :: Template :: Error: undefined method `name'for nil: NilClass
Rails error undefined method ʻimage_name'for nil: NilClass handling
[Ruby on Rails] undefined method ʻid'for nil: NilClass error resolution method
RSpec error resolution undefined method `feature'for RSpec: Module
[Ruby on Rails] Follow function undefined method ʻid'for nil: NilClass error resolution
I got an error "undefined method` create'" in RSpec
[RSpec] When I tested the view file that used the helper method, I got ActionView :: Template :: Error :. ..
Undefined local variable or method error in partial template collection option
RSpec error resolution undefined method'name' in'user' factory
About the relationship between Rails credentials.yml.enc and master.key (undefined method `[]'for nil: NilClass (NoMethodError))
[Rails] How to solve the error "undefined method` visit'" when using Capybara with Rspec