Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7834723
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T13:17:12+00:00 2026-06-02T13:17:12+00:00

i am new to rails testing and i am using unit:test. I have an

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T13:17:18+00:00Added an answer on June 2, 2026 at 1:17 pm

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new to rails and have just started writing tests using rspec version
I'm new to unit/func testing in Rails 3. So I'm starting now, better late
I am running my tests for a rails application using test/unit and capybara. I
I'm getting a 403 error on my new rails testing page: removed.com:8080. I installed
Starting a new rails project and we have a well-thought-out color palette, and want
Running the command generates new rails projects: $ rails generate controller home index The
I'm currently trying to set up integration/acceptance testing for a new rails 3 application
I've written functional tests for API endpoints built in Rails using shoulda testing framework.
I'm currently finishing up testing a new Ruby on Rails app. Just recently, some
I am new to Rails Testing . Can anybody suggest a good reference link

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.