-
-
Save karmi/25475 to your computer and use it in GitHub Desktop.
Rails' `assigns` helper for Sinatra tests by lifo
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
$: << "/Users/lifo/Rails/sinatra/lib" | |
require 'rubygems' | |
require 'sinatra' | |
require 'sinatra/test/unit' | |
require 'app' | |
class Rack::Response | |
attr_accessor :variables | |
end | |
class Rack::MockResponse | |
attr_accessor :variables | |
def initialize_with_variables(status, headers, body, errors=StringIO.new("")) | |
self.variables = body.variables if body.respond_to?(:variables=) | |
initialize_without_variables(status, headers, body, errors) | |
end | |
alias_method :initialize_without_variables, :initialize | |
alias_method :initialize, :initialize_with_variables | |
def assigns(name) | |
variables["@#{name}"] | |
end | |
end | |
class Sinatra::EventContext | |
def finish | |
result = super | |
self.response.variables = {} | |
instance_variables.each do |var| | |
self.response.variables[var] = instance_variable_get(var) | |
end | |
result | |
end | |
end | |
class AppTest < Test::Unit::TestCase | |
def test_my_default | |
get_it '/' | |
assert_equal 'Hello Templates', @response.body | |
assert_equal 1, @response.assigns(:hello) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sinatra::EventContext does not exist in latest versions, so this example is outdated.