Accessing controller instance variables from within an rspec controller spec

#assigns is deprecated. Here's a solution that avoids pulling in the rails-controller-testing gem

Though it can be a code smell to test instance variables in controllers, to upgrade older apps you can leverage #instance_variable_get.

Rspec example: expect(@controller.instance_variable_get(:@person).id).to eq(person_id)


In Rails 5, assigns has now been split out into a new rails-controller-testing gem.

You can either install the gem to continue using assigns, or you could use what the rails-controller-testing gem uses internally, which@controller.view_assigns[].


Use the assigns method (*note: assigns is now deprecated. See bottom of my answer for info):

it "assigns the @widget"
  expect(assigns(:widget)).not_to be_nil
end

Of course, you can inspect widget as you'd like, but without seeing what @widget is from your provided controller code, I just checked if it was nil

If you're wanting to puts the widget, like in your example, just print it with assigns

puts assigns(:widget)

EDIT: assigns is now deprecated (see: https://apidock.com/rails/ActionController/TestProcess/assigns)

If you want to continue using assigns you will need to install the rails-controller-testing gem.

Otherwise, you could use what rails-controller-testing gem uses internally: @controller.view_assigns[]