2

i am new to rails testing and i am using unit:test. I have an action in my controller

 def save_campaign unless params[:app_id].blank? @app = TestApp.find(params[:app_id]) if params[:test_app] @app.update_attributes(params[:test_app]) end flash[:notice] = "Your Registration Process is completed" redirect_to "/dashboard" else redirect_to root_path end end 

and my test case is as following

test "should save campagin " do assert_difference('TestApp.count', 0) do post :save_campaign, test_app: @test_app.attributes end assert_redirected_to "/dashboard" end end 

This method is a post method. While running this test, it is failing and showing me a message

"should save campagin (0.07s) Expected response to be a redirect to http://test.host/dashboard but was a redirect to http://test.host/ /home/nouman/.rvm/gems/ruby-1.9.2-p290@global/gems/actionpack-3.1.3/lib/action_dispatch/testing/assertions/response.rb:67:in `assert_redirected_to'

My guess is that i am not giving it right assertion to check params

params[:app_id] and @app = TestApp.find(params[:app_id]).

How can i write such an assertion to check these attributes, check wether a parameter is blank. How can 1 find an object with a given id.

1 Answer 1

1

For functional test, you should not care about testing the model, that is in your case, you should remove:

assert_difference('TestApp.count', 0) do .. end 

What you want to know in a functional test is that if the page is loaded, redirected correctly.

In your controller, you have a condition check for params, so for each of the outcome of the check, you write a test each, that is you have to write two functional tests:

test "if app_id param is empty, #save_campaign redirect to root" do post :save_campaign, :app_id => nil assert_redirected_to root_path end test "#save_campaign" do post :save_campaign, :app_id => app_fixture_id, :test_app => @test_app.attributes.to_params assert_redirected_to '/dashboard' end 

The trick to prepare the post params is to use method to_params method.

Hope this help.

UPDATE: If you just want to check if params[:app_id] GET param is in the URL, you should just check for this presence instead of checking if it is blank or not:

if params[:app_id] else end 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Trung for the answer, sorry i was not aware of your response that's why accepting it after few months.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.