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.
For functional test, you should not care about testing the model, that is in your case, you should remove:
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:
The trick to prepare the post params is to use method
to_paramsmethod.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: