1

In an Rspec controller test I want to check the values of instance variables in my the controller that I am testing.

For example,

controller:

class ThingsController < ApplicationController
  def index
    @things = Thing.all
  end
end

spec:

RSpec.describe UsersController, type: :controller do
  describe 'GET #index' do 
    expect(@things).to_not be_nil
  end
end

How do I access @things in the above example?

1 Answer 1

2

This should do the trick:

expect(assigns(:things)).to_not be_nil

More likely you'll want something like:

let(:thing) { ... }
...
expect(assigns(:things)).to eq([thing])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.