I'm using Rails 5.2 with rspec-rails 3.7 and rails-controller-testing gems.
I have a controller that filters results on the index action (yeah, bad practice, legacy code, etc). The problem is I need to test that when I do GET work_orders/index params: { keywords: 'some word' }
, the instance variable @work_orders returns filtered results, but if I use assigns(:work_orders)
it returns nil. I even tested this assigning the last WorkOrder to that variable, but it still doesn't show the variable in the hash.
work_orders_controller.rb
def index
... some code ...
@work_orders = WorkOrder.last
respond_to do |format|
format.html
end
end
spec/controllers/work_orders_controller_spec.rb
require 'rails_helper'
describe WorkOrdersController do
describe 'GET index' do
it 'collects work orders filtered by courier_ot in @work_orders' do
get :index
expect(assigns(:work_orders)).to be_an_instance_of WorkOrder
end
end
end
That is the simplest example I tested and it doesn't work. The assigns
call returns nil, and if I use byebug to inspect the hash, it only has this: {"marked_for_same_origin_verification"=>true}
.
The real test should use get :index, params: { keywords: 'asdf' }
and test that it gets the filtered work orders.
Any help would be appreciated. Thanks.