Rails: 在 rpec 测试中访问控制器实例变量
Rails: accessing controller instance variables in rpec test
在 Rspec 控制器测试中,我想检查我正在测试的控制器中实例变量的值。
例如,
控制器:
class ThingsController < ApplicationController
def index
@things = Thing.all
end
end
规格:
RSpec.describe UsersController, type: :controller do
describe 'GET #index' do
expect(@things).to_not be_nil
end
end
如何访问上例中的@things
?
这应该可以解决问题:
expect(assigns(:things)).to_not be_nil
您更有可能想要这样的东西:
let(:thing) { ... }
...
expect(assigns(:things)).to eq([thing])
在 Rspec 控制器测试中,我想检查我正在测试的控制器中实例变量的值。
例如,
控制器:
class ThingsController < ApplicationController
def index
@things = Thing.all
end
end
规格:
RSpec.describe UsersController, type: :controller do
describe 'GET #index' do
expect(@things).to_not be_nil
end
end
如何访问上例中的@things
?
这应该可以解决问题:
expect(assigns(:things)).to_not be_nil
您更有可能想要这样的东西:
let(:thing) { ... }
...
expect(assigns(:things)).to eq([thing])