public
Last active

Get access to the Sinatra application instance that runs the current test and mock it

  • Download Gist
gistfile1.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
require 'something/to/be/required'
 
class Sinatra::Base
@@prepared = nil
 
def self.onion_core
onion = prototype
loop do
onion = onion.instance_variable_get('@app')
return onion if onion.class == self || onion.nil?
end
end
 
def self.prepare_instance
@@prepared = onion_core
end
 
# Override
def call(env)
d = @@prepared || dup
@@prepared = nil
d.call!(env)
end
end
 
describe 'An Sinatra app' do
include Rack::Test::Methods
def app
Sinatra::Application
end
it 'mocks Kernel#open' do
file = {}
file.stub(:read).and_return('Happy New Year!')
a = app.prepare_instance
a.should_receive(:open).and_return(file)
get '/new_year'
end
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.