简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM