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 6933497
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:50:23+00:00 2026-05-27T11:50:23+00:00

I am trying to make a test for a controller for a nested resource.

  • 0

I am trying to make a test for a controller for a nested resource.

The nesting is like this in the routes.rb

resources :cars, :only => [:index, :destroy, :show] do
  resources :car_subscriptions, :only => [:new, :create], :as => :follow_subscriptions
end

I’m trying to test the create action most specifically:

describe CarSubscriptionsController do

  def valid_attributes
    {:car_id => '1', :user_id => '2'}
  end

  describe "POST create" do
    describe "with valid params" do
      it "creates a new CarSubscription" do
        expect {
          post :create, :car_id => 1, :car_subscription => valid_attributes
        }.to change(CarSubscription, :count).by(1)
      end

      it "assigns a newly created car_subscription as @car_subscription" do
        post :create, :car_subscription => valid_attributes
        assigns(:car_subscription).should be_a(CarSubscription)
        assigns(:car_subscription).should be_persisted
      end

      it "redirects to the created car_subscription" do
        post :create, :car_subscription => valid_attributes
        response.should redirect_to(CarSubscription.last)
      end
    end
  end

end

It’s actually a part of the scaffold generated by rails script. And I only modified the valid_attributes and the post in the first ‘it’

And the output is this:

  1) CarSubscriptionsController POST create with valid params creates a new CarSubscription
     Failure/Error: post :create, :car_id => 1, :car_subscription => valid_attributes
     ActionController::RoutingError:
       No route matches {:car_id=>"1", :car_subscription=>{:car_id=>"1", :user_id=>"2"}, :controller=>"car_subscriptions", :action=>"create"}
     # ./spec/controllers/car_subscriptions_controller_spec.rb:34:in `block (5 levels) in <top (required)>'
     # ./spec/controllers/car_subscriptions_controller_spec.rb:33:in `block (4 levels) in <top (required)>'

It’s the same error for all ‘it’s.

I’ve tried removing the :as => :following_subscriptions from the routes.rb file but the same problem.

I have actually split up the resources of car_subscriptions so index and destroy are in not nested, and create and new are nested in :cars

I don’t want to use hard coded paths like in this answer but if it is the only way, I can give it a try:

{ :post => "/forum_topics/1/forum_sub_topics" }.should route_to(:controller => "forum_sub_topics", :action => "create", :forum_topic_id => 1)

EDIT

Oh, and my rake routes looks like this:

car_follow_subscriptions_da POST   /biler/:car_id/car_subscriptions(.:format)                     {:action=>"create", :controller=>"car_subscriptions", :locale=>"da"}
  • 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-05-27T11:50:24+00:00Added an answer on May 27, 2026 at 11:50 am

    From what rake routes provides, I guess you should replace:

     post :create, :car_id => 1, :car_subscription => valid_attributes
    

    with:

     post :create, :car_id => 1, :car_subscription => valid_attributes, :locale => "da"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This one's really getting me down! :( I'm trying to make a nested model
I'm trying to test a controller and I got this error. I understand the
I am trying to make a Test Webservice and throw a SoapException. But when
I am trying to make a boolean test so that if one of the
I'm trying to test an application and I need to make an valid IP
I have been trying to test my application to make sure that all the
I'm trying to grade a quiz application I would like to make. I have
I'm trying to make test-program that opens a website by pushing a button. I
I've been trying to make a unit test for an application I'm writing. I
I am trying to test my controller's create action. I am using Devise for

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.