2012-08-02 3 views
0

내가 RSpec에와 함께 테스트하고 있습니다와 RoutingError 내 일정 문제는 ...이 내 RSpec에ActionController :: RSpec에

describe "POST 'create'" do 

    describe "success" do 

     before(:each) do 
      @user = User.create!(:email => "[email protected]", :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 

내 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 
.... 

업데이트를 실행하면

이 오류가 발생합니다.

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

내 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 

....

난 당신이 아직 아무것도

여기

을 나에게 준 없지만 솔루션을 편집 내 레이크 경로입니다

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 

답변

0

:cars 리소스가 내부에 중첩되어있는 것처럼 보입니다. 당신의 :users 자원 :이 방법을 당신의 경로를 구성하는 경우

resources :users do 
    resources :cars do 
... 

, 당신은 HTTP 작업을 호출 할 때뿐만 아니라 사용자를 지정해야합니다. 이 작동하지 않습니다

post :create, :car => @car 

당신이 컨트롤러에 액세스하십시오 :user_id, 실종 때문에 :

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

는이 문제를 해결하기를, 패스 :user_id 다음 모의 또는 스텁 User는 사용자를 반환 또는 모의 사용자.

는 UPDATE :

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 

를 삽입으로 사용자를 생성해야 최소한의 어떤 속성 :

그것은 망신 시켰 실제 기록을 섞어 일반적으로 좋은 방법은, 그래서 여기 아니다는 모의 객체없이 할 수있는 방법 User.create(...). 나는 그것이 그것을해야한다고 생각한다.

모의 작업을 수행하려는 경우 (일반적으로 컨트롤러/모델 사양을 분리 된 상태로 유지하는 것이 더 좋습니다) 여기에 a good starting point on how to do it이 있습니다.

+0

어떻게 사용자를 스터핑 할 수 있습니까? (buecause 나는 mockha를 사용하지 않는다) – Asantoya17

+0

RSpec은 스터 빙을 내장하고있다 : https://www.relishapp.com/rspec/rspec-mocks/docs – maxenglander

+0

추가 정보. @maxenglander가 지적했듯이 rspec에서 mock/stub을 사용할 수 있습니다. –