Testen verschachtelte Ressource controller mit rspec in Rails 3.1

Ich versuche, einen test für einen controller für eine verschachtelte Ressource.

Den Schachteln ist, wie dies in den Strecken.rb

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

Ich versuche zum testen der create-Aktion die meisten speziell:

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

Es ist eigentlich ein Teil des Gerüstes erzeugt, durch Schienen-Skript. Und ich habe nur geändert, das valid_attributes und die post in den ersten 'es'

Und die Ausgabe ist:

  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)>'

Es ist der gleiche Fehler, für all 'es.

Ich habe versucht, das entfernen der :as => :following_subscriptions aus den Routen.rb-Datei, aber das gleiche problem.

Habe ich eigentlich aufgeteilt, die Ressourcen der car_subscriptions so index und destroy sind nicht verschachtelt, und create und new verschachtelt sind :cars

Ich will nicht zu hart codierte Pfade wie in diesem Antwort aber wenn es der einzige Weg, ich kann es versuchen:

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

BEARBEITEN

Oh, und mein rake routes sieht wie folgt aus:

car_follow_subscriptions_da POST   /biler/:car_id/car_subscriptions(.:format)                     {:action=>"create", :controller=>"car_subscriptions", :locale=>"da"}
InformationsquelleAutor Cristian | 2011-12-13
Schreibe einen Kommentar