2016-12-07 84 views
0

我有一个items_controller.rb我需要帮助写在轨

def get_serialized_copy_of_item 
    @item= Item.find_by_id(params[:id]) 
    if @item.nil? 
     head :no_content 
    else 
     respond_to do |format| 
     serialized_item = @item.as_json(include: [:test1, :test2, :test3, :test4]) 
     format.html 
     format.json { render json: serialized_item } 
     end 
    end 
    end 

的routes.rb

namespace :items do 
    get '/get_serialized_copy_of_item/:id', to:'items#get_serialized_copy_of_item' 
end 

RSpec的测试我想写一个rspec的测试

  1. 提交不正确的商品ID并确保返回204

我已经做了

require 'spec_helper' 

describe Items::ItemsController do 
    describe "GET items#get_serialized_copy_of_item" do 
    it "renders 204 status code" do 
     get "/items/get_serialized_copy_of_item/dfsdf" 
     expect(last_response.status).to eq(204) 
    end 
    end 
end 

错误:我收到路由错误

F 

Failures: 

    1) Items::ItemsController GET items#item renders 204 status code 
    Failure/Error: get "/items/get_serialized_copy_of_item/dfsdf" 
    ActionController::RoutingError: 
     No route matches {:controller=>"items/items", :action=>"/items/get_serialized_copy_of_item/dfsdf"} 
    # ./spec/controllers/items/items_controller_spec.rb:6:in `block (3 levels) in <top (required)>' 

Finished in 0.01576 seconds 
1 example, 1 failure 

Failed examples: 

rspec ./spec/controllers/items/items_controller_spec.rb:5 # Items::itemsController GET items#item renders 204 status code 

束EXEC耙路线

         GET `items/get_serialized_copy_of_item/:id(.:format)       items/items#get_serialized_copy_of_item` 

感谢

+0

可以请你'束EXEC耙routes'的输出添加到您问题的结束? – sixty4bit

+0

@ sixty4bit新增。 – kavin

回答

3

RSpec的假设控制器名称从名字的测试。注意this doc的例子:

describe WidgetsController do 
    describe "GET index" do 
    it "has a 200 status code" do 
     get :index 
     response.code.should eq("200") 
    end 
    end 
end 

这里的RSpec已经知道,因为describe WidgetsController路径的widgets部分,所以get方法只需要:index作为参数。

要翻译,为您的情况:

describe Items::ItemsController do 
    describe "GET get_serialized_copy_of_item" do 
    it "renders 204 status code" do 
     get :get_serialized_copy_of_item, id: 'sdf' 

     expect(response.status).to eq(204) 
    end 
    end 
end 
  1. 你只需要在动作的名称传递给get()方法,而不是整个路径。
  2. 您需要包括:id PARAM作为第二个参数get()
  3. 好像namespace是错误的方式在这里做路由。试着改变你的路由定义这样的:
resources :items do 
    member do 
    get :get_serialized_copy 
    end 
end 

,这将产生以下路线:

➜ bundle exec rake routes 
    Prefix Verb URI Pattern  

    Controller#Action 
get_serialized_copy_item GET /items/:id/get_serialized_copy(.:format) items#get_serialized_copy 

为此,您希望您的ItemsControllerapp/controllers/items_controller.rb

+0

我试过,但我得到同样的错误。我也试过'描述ItemsController' – kavin

+0

这个答案对我来说是正确的。你仍然得到这个:'没有路线匹配{:controller =>“items/items”,:action =>“/ items/get_serialized_copy_of_item/dfsdf”}'? –

+0

@JagdeepSingh,是'ActionController :: RoutingError: 没有路由匹配{:id =>“sdf”,:controller =>“items/items”,:action =>“get_serialized_copy_of_item”} – kavin

0

从我所看到的没有任何嵌套。

如果是这样,您应该致电ItemsController而不是Items::ItemsController

items_controller_spec:

require 'spec_helper' 

describe ItemsController do 
    describe "GET #get_serialized_copy_of_item" do 
    it "renders 204 status code" do 
     get "/items/get_serialized_copy_of_item/dfsdf" 
     expect(last_response.status).to eq(204) 
    end 
    end 
end