Skip to content

Instantly share code, notes, and snippets.

@shuhei
Created December 27, 2011 11:25
Show Gist options
  • Save shuhei/1523353 to your computer and use it in GitHub Desktop.
Save shuhei/1523353 to your computer and use it in GitHub Desktop.
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