Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Get access to the Sinatra application instance that runs the current test and mock it
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment