Wrong previous stub implementation overriding warning emission
Created by: mdesantis
Ruby 2.1.2, RSpec 3.0.0, RSpec Mocks 3.0.4
I get a WARNING: You're overriding a previous stub implementation of 'call'
that I think shouldn't be emitted. I guess it is wrong because it's the only .and_call_original
I have.
This is the output of rspec spec.rb
:
WARNING: You're overriding a previous stub implementation of `call`. Called from /home/izietto/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/rack-1.5.2/lib/rack/config.rb:17:in `call'.. Called from /home/izietto/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/rack-1.5.2/lib/rack/config.rb:17:in `call'.
.
Finished in 0.01976 seconds (files took 6.25 seconds to load)
1 example, 0 failures
This is the spec.rb
(you can jump straight to the .and_call_original
call I guess, the rest is just for make the test pass):
unless File.exist?('Gemfile')
File.write('Gemfile', <<-GEMFILE)
source 'https://rubygems.org'
gem 'rack'
gem 'rack-test'
gem 'rspec'
GEMFILE
system 'bundle'
end
require 'bundler'
Bundler.setup(:default)
require 'rack/test'
require 'rack'
require 'forwardable'
module TestHelpers
extend Forwardable
def_delegators :last_response, :body, :headers, :status, :errors
def_delegators :current_session, :env_for
attr_writer :app
def app
@app ||= nil
@app || mock_app(DummyApp)
end
def mock_app(app = nil, &block)
app = block if app.nil? and block.arity == 1
if app
klass = described_class
mock_app do
use Rack::Head
use(Rack::Config) { |e| e['rack.session'] ||= {}}
use klass
run app
end
else
@app = Rack::Lint.new Rack::Builder.new(&block).to_app
end
end
end
module DummyApp
def self.call(env)
Thread.current[:last_env] = env
body = (env['REQUEST_METHOD'] == 'HEAD' ? '' : 'ok')
[200, {'Content-Type' => env['wants'] || 'text/plain'}, [body]]
end
end
RSpec.configure do |config|
config.include Rack::Test::Methods
config.include TestHelpers
end
a_rack_application = Struct.new(:app) do
def call(env)
app.call(env)
end
end
RSpec.describe a_rack_application do
it 'example' do
klass = described_class
detector = Struct.new(:app) do
def call(env)
app.call(env)
end
end
changer = Struct.new(:app) do
def call(env)
env['foo.bar'] = 42
app.call(env)
end
end
expect_any_instance_of(detector).to receive(:call) { |env|
expect(env['foo.bar']).to eq 42
}.and_call_original
mock_app do
use Rack::Head
use(Rack::Config) { |e| e['rack.session'] ||= {}}
use detector
use klass
use changer
run DummyApp
end
expect(get('/')).to be_ok
end
end