2012-07-30 86 views
0

林测试我的应用程序使用RSpec下一个简单的代码:的ActiveRecord :: RecordNotFound:

require 'spec_helper' 

    describe CarsController do 
    describe "GET 'new'" do 
      it "should be successful" do 
      visit new_user_car_path(:user_id=>"28") 
      response.should be_success 
      end 
     end 
    end 

当我运行它,我得到这个消息

Failure/Error: visit new_user_car_path(:user_id=>"28") 
ActiveRecord::RecordNotFound: 
    Couldn't find User with id=28 
# ./app/controllers/cars_controller.rb:3:in `new' 
# ./spec/controllers/tanking_logs_controller_spec.rb:6:in `block (3 levels) in <top (required)>' 

我不知道什么会发生与这一点,在我的路线出现new_user_car_path,看...

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 

,这是我的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

只是为了排除简单的东西,你有没有尝试重新启动服务器?或者你是否有route.rb中的任何路径是“挂载”的? – Catfish 2012-07-30 20:15:50

+0

是的,我重启了很多次 – Asantoya17 2012-07-30 20:18:40

回答

1
new_user_car GET /users/:user_id/cars/new(.:format) 

你的路径需要为了工作:user_id PARAM,和你没有提供的。

编辑根据您的意见,如果你使用Factory Girl Rails你可以建立一个工厂来创建用户,然后这样做:

user = Factory.create :user 
visit new_user_car_path(:id => user.id) 

否则,您可以手动创建它们:

user = User.create!(:name => "Joe", ...) 
visit new_user_car_path(:id => user.id) 
+0

我尝试这样做: 访问new_user_car_path(:USER_ID => “28”) 但是当我运行我的测试表明: 故障/错误:参观new_user_car_path(:USER_ID =>” 28“) ActiveRecord :: RecordNotFound: 找不到id = 28的用户 – Asantoya17 2012-07-30 20:49:14

+0

您是否随机键入”28“?你需要一个真实的用户ID。显然它正在为该用户进行搜索。你应该看看你的汽车控制器,你会看到它进行搜索。 – MrDanA 2012-07-30 22:03:02

+0

user_id 28存在 – Asantoya17 2012-07-30 22:12:06

相关问题