0
votes

I'm trying to test this controller but an error is returning.

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

Failures:

1) Api::V1::UsersController GET #show returns the information about a reporter on a hash Failure/Error: get :show, :id => @user.id, format: :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 Failure/Error: get :show, :id => @user.id, format: :json

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

Finished in 0.05594 seconds (files took 2.35 seconds to load) 2 examples, 2 failures

Failed examples:

rspec ./spec/controllers/api/v1/users_controller_spec.rb:12 # Api::V1::UsersController GET #show returns the information about a reporter on a hash rspec ./spec/controllers/api/v1/users_controller_spec.rb:17 # Api::V1::UsersController GET #show

1
Controller specs are deprecated, FYI. You should switch to using request specs for new tests.meagar♦

1 Answers

0
votes

You should be passing a params hash containing the parameters you're interested in submitting:

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