Stub method error in request specification

I am studying a RoR tutorial (Ruby-2.1, Rails 4.x) with this book .

This is a great book to follow, but I got this problem in the rspec test in chapter 5 . (See Listing 5.9 in this chapter.)

Failure/Error: authentication.stub<:request>.and_return<request> #<Authentication:0x000000075fe220> does not implement: request 

Source:

 class Authentication include Authenticable end describe Authenticable do let(:authentication) { Authentication.new } describe "#current_user" do before do @customer = FactoryGirl.create :customer request.headers["Authorization"] = @customer.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 @customer.auth_token end end end 

How to solve this problem?

+7
ruby ruby-on-rails rspec
source share
5 answers

If you want to use rspec 3, perhaps you can replace the code with the following:

specifications / controllers / problems / authenticable_spec.rb

 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 

application / controllers / problems / 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 

pdt: there is an error using the rspec controller helper method. more links https://github.com/rspec/rspec-rails/issues/1076

+5
source share

I am the author of the book, which version of RSpec are you using? You will probably have to set it to 2.14 as follows:

 group :test do gem "rspec-rails", "~> 2.14" end 

Let me know how this happens!

+1
source share

I would try to complete the project using "rspec-rails", "~> 2.14" as suggested.

Once you finish the tutorial, you can use transpec, a gem that will update your tests for compatibility with rspec3.

https://github.com/yujinakayama/transpec

Install the gem and update the project> gemfile to "rspec-rails", "~> 3.1.0" and run transpec in the project.

This should update all your tests.

I think this is what your test should look like using rspec 3

 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 

hope this helps.

0
source share

Just fill in the Authentication class in the specification as follows:

 class Authentication include Authenticable attr_accessor :request, :response end 
0
source share

I found using this github issue # 4 I was able to solve this by simply changing:

 class Authentication 

in the authenticable_spec.rb file:

 class Authentication < ActionController::Base 

This means that the Authentication class inherits the ActionController::Base request method.

0
source share

All Articles