Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails RubyonRails4:在rspec不工作的情况下测试嵌套资源_Ruby On Rails_Rspec_Mongoid - Fatal编程技术网

Ruby on rails RubyonRails4:在rspec不工作的情况下测试嵌套资源

Ruby on rails RubyonRails4:在rspec不工作的情况下测试嵌套资源,ruby-on-rails,rspec,mongoid,Ruby On Rails,Rspec,Mongoid,我在测试RubyonRails应用程序时遇到了一些问题。我有一个资源餐厅和嵌套的资源菜单 路由文件: resources :restaurants do resources :menus 菜单模式: class Menu include Mongoid::Document belongs_to :restaurant accepts_nested_attributes_for :restaurant validates :name, presence: true validates :

我在测试RubyonRails应用程序时遇到了一些问题。我有一个资源餐厅和嵌套的资源菜单

路由文件:

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)>'
有趣的是,这个测试在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
我真的不知道为什么这个测试不起作用(

请..我需要一些解释..如果有人能帮忙..请:) 也欢迎阅读以下资源:)


非常感谢..

好的,我知道了,测试应该是这样的:

 it 'renders the index template' do
  get :index, { :restaurant_id => @restaurant  }
  expect(response).to render_template('index')
end

我必须加上:餐厅id。。然后它成功了

在测试
中键入:controller
当我执行
获取餐厅菜单路径(@restaurant)
时,抛出此错误:

ActionController::UrlGenerationError:
   No route matches {:action=>"/restaurant/1/menus", :controller=>"restaurants"}
在方法get的测试类型控制器中,第一个参数不是控制器中的操作:

因此,它适用于:

get :index, { :restaurant_id => @restaurant  }

有没有可能指向控制器?我遇到了类似的问题,但我得到了
#抱歉,我在度假。。我在模型上贴了标签。菜单模型有如下内容:接受\u嵌套的\u属性\u for:restaurant和我的餐厅模型:接受\u嵌套的\u属性\u for:menusThanks。我解决了。我不记得问题出在哪里,但我相信这与“子”资源(在工厂中)上必须存在的关联有关,否则它们将被视为“非关联”资源。
get :index, { :restaurant_id => @restaurant  }