kurenn / market_place_api

The API on Rails tutorial application
142 stars 68 forks source link

Listing 5.12 #83

Closed gamalielhere closed 7 years ago

gamalielhere commented 7 years ago

Hello,

Not sure what's happening but I'm getting these errors on rspec:


  1) Authenticable#authenticate_with_token renders a json error message
     Failure/Error: response.stub(:response_code).and_return(401)

     ArgumentError:
       wrong number of arguments (given 2, expected 1)
     # ./spec/controllers/concerns/authenticable_spec.rb:27:in `block (3 levels) in <top (required)>'

  2) Authenticable#authenticate_with_token 
     Failure/Error: response.stub(:response_code).and_return(401)

     ArgumentError:
       wrong number of arguments (given 2, expected 1)
     # ./spec/controllers/concerns/authenticable_spec.rb:27:in `block (3 levels) in <top (required)>'

Deprecation Warnings:

Using `stub` from rspec-mocks' old `:should` syntax without explicitly enabling the syntax is deprecated. Use the new `:expect` syntax or explicitly enable `:should` instead. Called from /Users/gamalielpadillo/workspace/rich-honey-apparel/rich-honey-api/spec/controllers/concerns/authenticable_spec.rb:15:in `block (3 levels) in <top (required)>'.

If you need more of the backtrace for any of these deprecations to
identify where to make the necessary changes, you can configure
`config.raise_errors_for_deprecations!`, and it will turn the
deprecation warnings into errors, giving you the full backtrace.

1 deprecation warning total

Finished in 0.6252 seconds (files took 7.54 seconds to load)
32 examples, 2 failures

Failed examples:

rspec ./spec/controllers/concerns/authenticable_spec.rb:32 # Authenticable#authenticate_with_token renders a json error message
rspec ./spec/controllers/concerns/authenticable_spec.rb:36 # Authenticable#authenticate_with_token 

authenticable_spec.rb:

require 'rails_helper'

class Authentication < ActionController::Base
  include Authenticable
end

describe Authenticable do
  let(:authentication) { Authentication.new }
  subject { authentication }

  describe '#current_user' do
    before do
      @user = FactoryGirl.create(:user)
      request.headers['Authorization'] = @user.auth_token
      authentication.stub(:request).and_return(request)
    end

    it 'returns the user from the authorization header' do
      expect(authentication.current_user.auth_token).to eql @user.auth_token
    end
  end

  describe '#authenticate_with_token' do
    before do
      @user = FactoryGirl.create(:user)
      authentication.stub(:current_user).and_return(nil)
      response.stub(:response_code).and_return(401)
      response.stub(:body).and_return({ 'errors' => 'Not authenticated' }.to_json)
      authentication.stub(:response).and_return(response)
    end

    it 'renders a json error message' do
      expect(json_response[:errors]).to eql 'Not authenticated'
    end

    it { should respond_with 401 }
  end
end
hiendinhngoc commented 7 years ago

@gamalielhere please try this: allow(response).to receive(:response_code).and_return(401) instead of response.stub(:response_code).and_return(401)

gamalielhere commented 7 years ago

@hiendinhngoc I fixed it by changing the whole thing with a snippet I found from a russian site. I'll source it later if I find it.

kurenn commented 7 years ago

@gamalielhere Can you post your answer? it would be interesting

@hiendinhngoc Thanks for the response, the rspec version on that time allowed me to do that, but you are right!