Created
December 27, 2011 11:25
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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