【问题标题】:Ruby on Rails 4: test nested resources with rspec not workingRuby on Rails 4:使用 rspec 测试嵌套资源不起作用
【发布时间】:2013-12-06 18:03:34
【问题描述】:

我在测试我的 ruby​​ on rails 应用程序时遇到了一些麻烦。我有一个资源餐厅和一个嵌套的资源菜单。

路线文件:

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

我真的不知道为什么这个测试不起作用.. :(

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

非常感谢..

【问题讨论】:

    标签: ruby-on-rails rspec mongoid


    【解决方案1】:

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

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

    我必须添加 :restaurant_id.. 然后它起作用了。

    【讨论】:

    • 你有机会指点控制器吗?我遇到了类似的问题,但我收到了#&lt;Mongoid::Criteria。不知道为什么!另外,您如何在您的 before 块上创建两个不同的实例,您在哪里声明两者是关联的?
    • 抱歉我在度假。我在模型上贴了它。菜单模型是这样的:accepts_nested_attributes_for :restaurant 和我的餐厅模型:accepts_nested_attributes_for :menus
    • 谢谢。我解决了。我不记得问题出在哪里,但我认为它必须与“子”资源(在工厂中)上必须存在的关联有关,否则它们将被视为“未关联的”资源。
    【解决方案2】:

    在测试type:controller当我做get restaurant_menus_path(@restaurant)时抛出这个错误:

    ActionController::UrlGenerationError:
       No route matches {:action=>"/restaurant/1/menus", :controller=>"restaurants"}
    

    在测试类型控制器方法中获取的第一个参数不是控制器中的动作:

    所以它适用于:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-25
      • 2023-03-29
      • 1970-01-01
      • 1970-01-01
      • 2012-01-19
      • 1970-01-01
      相关资源
      最近更新 更多