Home > front end >  RSpec integration testing -- instance variable in application controller is nil
RSpec integration testing -- instance variable in application controller is nil

Time:10-05

Recently started a new job, no ruby/rails experience before this gig so bare with me.

Currently I have a method defined in my application controller which has a few instance variables defined, and is being called :before_action do_something_on_start There is an instance variable that grabs an entry from our database and I set some instance variables that will be available in the view

def do_something_on_start
@test = User.find_by(id: 1)
@enabled = @test.value == "true"
@start_date = @test.start_date
end

When running our suite of test specs, most of the feature specs come back with undefined method `value' for nil:NilClass

Not entirely sure why this is happening. Are the instance variables not available to the test spec when they run? Or do I have to have those same instance variables defined in all of our feature specs to work?

CodePudding user response:

The error message is telling you that @test is nil, which means your DB doesn't contain a User with id 1. Generally test data is reset between each test, so that tests are isolated and order independent. Because of that you need to look at how you're setting up your test data either via fixtures, as mentioned by @arieljuod, or via factories using something like FactoryBot.

  • Related