2

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"}
0

1 Answer 1

10

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"
Sign up to request clarification or add additional context in comments.

7 Comments

Yeah! You're right, that's strange, this should be better documented in RubyOnRails guides !
I'd say you should set a default locale setter in your application controller
In my application, I18n.locale is available even if not set in the ApplicationController as described in the link. Means the application works even if the locale is not provided in the URL, but the tests fail...
If I add it to the controller, then remove it from the rspec, it still throws the RoutingError
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.