我需要帮助在rails中编写rspec测试

时间:2016-12-07 00:18:25

标签: ruby-on-rails ruby ruby-on-rails-3 rspec

我有一个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测试

  1. 提交错误的商品ID并确保返回204
  2. 我已经完成了

    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 rake路线

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

    由于

2 个答案:

答案 0 :(得分: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

由于widgets,RSpec已经知道路径的describe WidgetsController部分,因此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参数作为第二个参数包含在get()
  3. 这里似乎namespace是错误的路由方式。尝试将路线定义更改为:
  4. 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
    

    为此,您希望将ItemsController放在app/controllers/items_controller.rb

答案 1 :(得分: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