How to check parameters passed to controller in rails 3 using rspec?

Our code:

describe "GET show" do it "assigns the requested subcategory as @subcategory" do subcategory = Subcategory.create! valid_attributes get :show, :id => subcategory.id.to_s assigns(:subcategory).should eq(subcategory) end it "has a sort parameter in the url" do subcategory = Subcategory.create! valid_attributes get :show, {:id => subcategory.id.to_s, :params => {:sort => 'title'}} helper.params[:sort].should_not be_nil end end 

The following error message appeared:

 1) SubcategoriesController GET show has a sort parameter in the url Failure/Error: helper.params[:sort].should_not be_nil NameError: undefined local variable or method `helper' for #<RSpec::Core::ExampleGroup::Nested_4::Nested_2:0x007f81a467c848> # ./spec/controllers/subcategories_controller_spec.rb:54:in `block (3 levels) in <top (required)>' 

How to check parameters in rspec?

+53
ruby-on-rails rspec
Nov 16 2018-11-11T00:
source share
1 answer
 get :show, {:id => subcategory.id.to_s, :params => {:sort => 'title'}} 

Must be

 get :show, :id => subcategory.id.to_s, :sort => 'title' 

If you do not want to pass params[:params][:sort] .

Besides

 helper.params[:sort].should_not be_nil 

Must be

 controller.params[:sort].should_not be_nil controller.params[:sort].should eql 'title' 

(If you want to check the helper, you have to write an auxiliary specification.)

+103
Nov 25 '11 at 20:11
source share



All Articles