失败/错误:get:show,id:@ user.id,format :: json

时间:2018-02-14 13:30:25

标签: ruby-on-rails rspec

我正在尝试测试此控制器,但错误正在返回。

class Api::V1::UsersController < ApplicationController
  respond_to :json

  def show
    respond_with User.find(params[:id])
  end

end

require 'rails_helper'

RSpec.describe Api::V1::UsersController, type: :controller do
  before(:each) { request.headers['Accept'] = "application/vnd.marketplace.v1" }

  describe "GET #show" do
    before(:each) do
      @user = FactoryBot.create :user
      get :show, :id => @user.id, format: :json
    end

    it "returns the information about a reporter on a hash" do
      user_response = JSON.parse(response.body, symbolize_names: true)
      expect(user_response[:email]).to eql @user.email
    end

    it { should respond_with 200 }
  end
end

bundle exec rspec spec / controllers / api / v1 / users_controller_spec.rb

故障:

1)Api :: V1 :: UsersController GET #show返回有关哈希记者的信息      失败/错误:get:show,:id =&gt; @ user.id,格式:: json

 ArgumentError:
   unknown keyword: id
 # ./spec/controllers/api/v1/users_controller_spec.rb:9:in `block (3 levels) in <top (required)>'

2)Api :: V1 :: UsersController GET #show      失败/错误:get:show,:id =&gt; @ user.id,格式:: json

 ArgumentError:
   unknown keyword: id
 # ./spec/controllers/api/v1/users_controller_spec.rb:9:in `block (3 levels) in <top (required)>'

以0.05594秒结束(文件加载时间为2.35秒) 2个例子,2个失败

失败的例子:

rspec ./spec/controllers/api/v1/users_controller_spec.rb:12#Api :: V1 :: UsersController GET #show返回有关哈希记者的信息 rspec ./spec/controllers/api/v1/users_controller_spec.rb:17#Api :: V1 :: UsersController GET #show

1 个答案:

答案 0 :(得分:0)

您应该传递一个params哈希,其中包含您有兴趣提交的参数:

get :show, params: { id: @user.id }