ActionController::RoutingError with rspec

I'm testing with rspec and my constant problem is with the routes this is my rspec

describe "POST 'create'" do

    describe "success" do

        before(:each) do
            @user = User.create!(:email => "foo@example.com", :password => "foobar", :password_confirmation => "foobar" )
            @car = Car.create!(:brand => "example", :color => "foobar", :model => "foobar", :year =>"2012")
        end

        it "should create a car" do
            lambda do
            post :create, :cars => @car, :user_id => @user.id
            end.should change(Car, :count).by(1)
        end

        it "should redirect to the user welcome page" do
            post :create, :cars => @car, :user_id => @user.id
            response.should redirect_to user_car_path
        end
    end
end

my routes.rb ...

Estaciones::Application.routes.draw do
root :to => "static_pages#home"
match '/contact', :to=>'static_pages#contact'
match '/about', :to=>'static_pages#about'
devise_for :users
resources :gas_stations
resources :users do
  resources :cars do
    resources :tanking_logs
  end
end
....

UPDATE

when I run the test, got this error:

1) CarsController POST 'create' success should create a car
 Failure/Error: post :create, :cars => @car, :user => @user
 ActionController::RoutingError:
   No route matches {:cars=>"12", :user=>"74", :controller=>"cars", :action=>"create"}
 # ./spec/controllers/car_controller_spec.rb:22:in `block (5 levels) in <top (required)>'
 # ./spec/controllers/car_controller_spec.rb:21:in `block (4 levels) in <top (required)>'

2) CarsController POST 'create' success should redirect to the user welcome page
 Failure/Error: post :create, :cars => @car, :user => @user
 ActionController::RoutingError:
   No route matches {:cars=>"13", :user=>"75", :controller=>"cars", :action=>"create"}
 # ./spec/controllers/car_controller_spec.rb:27:in `block (4 levels) in <top (required)>'

here is my CarsController

class CarsController < ApplicationController
def new
  @user = User.find(params[:user_id])
  @car = @user.cars.build
end

def create
  @user = User.find(params[:user_id])
  @car = @user.cars.build(params[:car])
  if @car.save
    redirect_to user_car_path(@user, @car), :flash => { :notice => "  car created!" }
  else
    redirect_to new_user_car_path ,:flash => { :notice => " sorry try again :(" }
  end
end

....

I edited it with the solutions that you gave to me but still nothing

here is my rake routes

user_cars GET    /users/:user_id/cars(.:format)                               cars#index
                      POST   /users/:user_id/cars(.:format)                               cars#create
         new_user_car GET    /users/:user_id/cars/new(.:format)                           cars#new
        edit_user_car GET    /users/:user_id/cars/:id/edit(.:format)                      cars#edit
             user_car GET    /users/:user_id/cars/:id(.:format)                           cars#show
                      PUT    /users/:user_id/cars/:id(.:format)                           cars#update
                      DELETE /users/:user_id/cars/:id(.:format)                           cars#destroy

Answers


It looks like you have your :cars resource nested inside your :users resource:

resources :users do
  resources :cars do
...

If you structure your routes this way, then you need to specify a user as well when calling http actions. This won't work:

post :create, :car => @car

because it's missing a :user_id, which you access in the controller:

@user = User.find(params[:user_id])

To solve this, pass a :user_id and then mock or stub User to return a user or mock user.

UPDATE:

It's not generally good practice to mix mocks and actual records, so here's how you could do it without mocks:

before(:each) do
  @user = User.create(...)
  @car = {:brand => "example", :color => "foobar", :model => "foobar", :year =>"2012" }
end

it "should create a car" do
  lambda do
    post :create, :car => @car, :user_id => @user.id
  end.should change(Car, :count).by(1)
end

it "should redirect to the user welcome page" do
  post :create, :car => @car, :user_id => @user.id
  response.should redirect_to user_car_path
end

Insert whatever minimal attributes you need to create a user into User.create(...). I think that should do it.

If you want to do this with mocks (which is generally better practice, to keep controller/model specs decoupled), here's a good starting point on how to do it.


Need Your Help

Creating CSS Global Variables : Stylesheet theme management

css variables

Is there a way to set global variables in css such as: