ThriceGood
ThriceGood

Reputation: 1703

Rails: accessing controller instance variables in rpec test

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?

Upvotes: 1

Views: 157

Answers (1)

Michael Kohl
Michael Kohl

Reputation: 66867

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])

Upvotes: 2

Related Questions