我有一些关于测试我的Ruby on Rails应用程序的困难。我有一个资源餐厅和一个嵌套的菜单资源。
路由文件:
菜单模型:
然后,我正在尝试这样做。这是rspec的默认测试,但由于我有嵌套的资源,所以我正在尝试进行修改。
但错误是:
这是路线:
有趣的是,menu_spec.rb 中的这个测试正常运行。
我真的不知道为什么这个测试不起作用。。:(
请...我需要一些解释...如果有人可以帮忙...欢迎提供阅读资源 :)
非常感谢你。
路由文件:
resources :restaurants do
resources :menus
菜单模型:
class Menu
include Mongoid::Document
belongs_to :restaurant
accepts_nested_attributes_for :restaurant
validates :name, presence: true
validates :description, presence: true
validates :restaurant, presence: true
field :name, type: String
field :description, type: String
end
餐厅模型:
class Restaurant
include Mongoid::Document
has_one :address, dependent: :destroy
has_many :menus, dependent: :destroy
accepts_nested_attributes_for :address, :menus
validates :name, presence: true
validates :description, presence: true
validates :address, presence: true
field :name, type: String
field :description, type: String
field :thumbnail, type: String
field :banner, type: String
end
然后,我正在尝试这样做。这是rspec的默认测试,但由于我有嵌套的资源,所以我正在尝试进行修改。
describe MenusController do
before :each do
@restaurant = FactoryGirl.create(:random_restaurant)
@menu = FactoryGirl.create(:menu)
end
describe 'GET index' do
it 'assigns all menus as @menus' do
get restaurant_menus_path(@restaurant)
assigns(:menus).should eq([menu])
end
end
describe 'GET all restaurants menus' do
it 'responds with 200' do
get :index, { :id => @restaurant }
expect(response).to be_success
expect(response.status).to eq(200)
end
end
但错误是:
1) MenusController GET index assigns all menus as @menus
Failure/Error: get restaurant_menus_path(@restaurant)
ActionController::UrlGenerationError:
No route matches {:controller=>"menus", :action=>"/en/restaurants/52a20e4c6561721131010000/menus"}
# ./spec/controllers/menus_controller_spec.rb:30:in `block (3 levels) in <top (required)>'
2) MenusController GET all restaurants menus responds with 200
Failure/Error: get :index, { :id => @restaurant }
ActionController::UrlGenerationError:
No route matches {:action=>"index", :id=>"52a20e4c6561721131060000", :controller=>"menus"}
# ./spec/controllers/menus_controller_spec.rb:37:in `block (3 levels) in <top (required)>'
这是路线:
restaurant_menus_path GET (/:locale)/restaurants/:restaurant_id/menus(.:format) menus#index
POST (/:locale)/restaurants/:restaurant_id/menus(.:format) menus#create
new_restaurant_menu_path GET (/:locale)/restaurants/:restaurant_id/menus/new(.:format) menus#new
edit_restaurant_menu_path GET (/:locale)/restaurants/:restaurant_id/menus/:id/edit(.:format) menus#edit
restaurant_menu_path GET (/:locale)/restaurants/:restaurant_id/menus/:id(.:format) menus#show
PATCH (/:locale)/restaurants/:restaurant_id/menus/:id(.:format) menus#update
PUT (/:locale)/restaurants/:restaurant_id/menus/:id(.:format) menus#update
DELETE (/:locale)/restaurants/:restaurant_id/menus/:id(.:format) menus#destroy
有趣的是,menu_spec.rb 中的这个测试正常运行。
require 'spec_helper'
describe 'Menu' do
before :each do
@restaurant = FactoryGirl.create(:random_restaurant)
@menu = FactoryGirl.create(:menu)
end
describe 'GET /menus' do
it 'responds with 200' do
get restaurant_menu_path(@restaurant, @menu)
expect(response).to be_success
expect(response.status).to eq(200)
end
end
describe 'GET all restaurants menus' do
it 'responds with 200' do
get restaurant_menus_path(@restaurant)
expect(response).to be_success
expect(response.status).to eq(200)
end
end
end
我真的不知道为什么这个测试不起作用。。:(
请...我需要一些解释...如果有人可以帮忙...欢迎提供阅读资源 :)
非常感谢你。
#<Mongoid::Criteria>
。不知道为什么!另外,在你的before
块中,你是如何创建两个不同的实例的,你在哪里声明这两个实例是相关联的? - Pablo Olmos de Aguilera C.