2016年3月4日 星期五

api problem Stub Method error in request spec

according to the tutorial
http://apionrails.icalialabs.com/book/chapter_five

to setting current_user will face a problem


Failure/Error: authentication.stub(:request).and_return(request)



http://stackoverflow.com/questions/26428882/stub-method-error-in-request-spec


require 'rails_helper'

class Authentication
  include Authenticable
end

describe Authenticable, :type => :controller do
  let(:authentication) { Authentication.new }

  describe "#current_user" do
    before do
      @user = FactoryGirl.create :user
      request.headers["Authorization"] = @user.auth_token
      allow(authentication).to receive(: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
end
app/controllers/concerns/authenticable.rb
module Authenticable
  # Devise methods overwrites
  def current_user
    @current_user ||= User.find_by(auth_token: request.headers['Authorization'])
  end

  def request
    request
  end
end

沒有留言:

張貼留言