2012-08-03 37 views
1

我测试我的应用程序以创建一个新的用户汽车,稍后用户创建新汽车应用程序必须重定向到user_car_path(我发布我的路线) :lambda do count应该已经被改变了1,但被改变了0

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 

所以我测试我的应用程序与此rspec的:

describe "POST 'create' car" do 

describe "car created 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 cars page" do 
    post :create, :cars => @car, :user_id => @user.id 
    response.should redirect_to user_car_path(@user, @car) 
    end 
end 
end 

,但我得到了2个错误

Failures: 

1) CarsController POST 'create' car car created success should create a car 
Failure/Error: lambda do 
    count should have been changed by 1, but was changed by 0 
# ./spec/controllers/car_controller_spec.rb:20 

2) CarsController POST 'create' car car created success should redirect to the user cars page 
Failure/Error: response.should redirect_to user_car_path(@user, @car) 
    Expected response to be a redirect to <http://test.host/users/115/cars/40> but was a redirect to <http://test.host/users/115/cars/new>. 
# ./spec/controllers/car_controller_spec.rb:27 

,但我的应用程序正常工作;这里是我的CarController

class CarsController < ApplicationController 
.... 

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 
def show 
    @user = User.find(params[:user_id])  
    @car = @user.cars.find(params[:id]) 
end 

.... 
+0

你已经创建了车(它保存到数据库)在你之前的步骤中,所以我猜它不会再被创建。尝试使用'@car = Car.new(...)'而不是'@car = Car.create!(...)'看看是否有帮助。 – Frost 2012-08-03 15:30:35

+0

谢谢你的回答,你能更清楚地解释我吗?我在哪里创造了汽车?我开始使用rspec – Asantoya17 2012-08-03 15:38:03

+0

@ Asantoya17在自己的代码中查看'@car = Car.create!',并阅读rails文档,了解为什么'create!'与'new'不同。这不是一个rspec的事情。 – radixhound 2012-08-03 15:44:47

回答

4

您正在数据库中创建汽车,而不是在before(:each)中创建汽车对象。你也通过参数:cars而不是:car。最后,我还亲自使用let。尝试这个。

describe "POST 'create' car" do 

    let(:user) { User.create!(:email => "[email protected]", :password => "foobar", :password_confirmation => "foobar") } 
    let(:car) { Car.new(:brand => "example", :color => "foobar", :model => "foobar", :year =>"2012")} 

    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 cars page" do 
    post :create, :cars => car, :user_id => user.id 
    # note: car doesn't have an ID, you have to fetch it from the db to get an id 
    response.should redirect_to user_car_path(user.id, user.cars.last.id) 
    end 
end 

最后一点,你会想看看Factory Girl

然后,你可以这样做,而不是:

let(:user){ FactoryGirl.create(:user) } 
let(:car) { FactoryGirl.build(:car) } # note: BUILD not CREATE 
+0

谢谢你的回答,但仍然没有任何结果 – Asantoya17 2012-08-03 16:01:57

+0

@Asantoya17什么是'没有'意思?当然,它不会给你完全相同的错误。 – radixhound 2012-08-03 16:20:39

+0

它不显示相同的错误,现在出现ActionController :: RoutingError :: action =>“show”,:controller =>“cars” – Asantoya17 2012-08-03 16:23:14

0

我想你想以下(:车=>:汽车和使用PARAMS这将使控制器中的汽车产生有效的我感到困惑,为什么要创建在规范汽车。因为这似乎是汽车创作的测试)

it "should create a car" do 
    lambda do 
     post :create, :car => {:brand => "example", :color => "foobar", :model => "foobar", :year =>"2012"}, :user_id => @user.id 
    end.should change(Car, :count).by(1) 
    end 

    it "should redirect to the user cars page" do 
    post :create, :car => {:brand => "example", :color => "foobar", :model => "foobar", :year =>"2012"}, :user_id => @user.id 
    response.should redirect_to user_car_path(@user, @car) 
    end 
+0

真的我在学习rspec也许这就是为什么我可以有错误 – Asantoya17 2012-08-03 15:35:18

0

我认为这是因为您的验证失败。要知道,如果它不能做到这一点,之前保存在你的控制器:

puts @car.valid? 

如果是假的,你的测试将失败,这是正常的。要设置为true,你可以这样做:

before(:each) do 
    ... 
    Car.any_instance.stub(:valid?).and_return(true) 
end 

您还可以使用存根和嘲笑返回实例为User.findCar.build。请参阅文档:https://www.relishapp.com/rspec/rspec-mocks/v/2-6/docs/method-stubs

+0

好的我解决了第一个错误,但仍然出现第二个错误“预期的响应是重定向到,但重定向到。“ – Asantoya17 2012-08-03 15:43:50

+0

尝试在测试中删除汽车的's'。尝试在你的控制器中添加'puts @ car'和'puts @ user'来检查它们是否不为零。您也可以在控制器中进入调试模式(例如撬)。 – Dougui 2012-08-03 16:08:52

+0

错误已更改ActionController :: RoutingError :: action =>“show”,:controller =>“cars” – Asantoya17 2012-08-03 16:17:37

相关问题