Module: Sinatra::Tests::TestCase
- Includes:
- Rack::Test::Methods, Rack::Utils
- Defined in:
- lib/sinatra/tests/test_case.rb
Overview
Sinatra::Tests::TestCase
Module to be include into Test::Unit::TestCase declaration in spec_helper.rb
class Test::Unit::TestCase
include Sinatra::Tests::TestCase
<snip...>
end
Instance Method Summary collapse
-
#body ⇒ Object
Short for
last_response.body
. -
#erb_app(view, options = {}) ⇒ Object
Flexible method to test the ERB output.
-
#haml_app(view, options = {}) ⇒ Object
Flexible method to test the HAML output.
-
#method_missing(name, *args, &block) ⇒ Object
Delegate other missing methods to response.
-
#mock_app(base = Sinatra::Base, &block) ⇒ Object
Sets up a Sinatra::Base subclass defined with the block given.
-
#respond_to?(symbol, include_private = false) ⇒ Boolean
Also check response since we delegate there.
-
#setup ⇒ Object
Declaration of Sinatra setup.
-
#status ⇒ Object
Short for
last_response.status
.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
Delegate other missing methods to response.
71 72 73 74 75 76 77 |
# File 'lib/sinatra/tests/test_case.rb', line 71 def method_missing(name, *args, &block) if response && response.respond_to?(name) response.send(name, *args, &block) else super end end |
Instance Method Details
#body ⇒ Object
Short for last_response.body
. Making it easier to work with the returned body of a response
54 55 56 |
# File 'lib/sinatra/tests/test_case.rb', line 54 def body response.body.to_s end |
#erb_app(view, options = {}) ⇒ Object
Flexible method to test the ERB output.
Accepts custom :layout & :url options passed.
Examples
erb_app "<%= some_method('value') %>"
body.should == 'some result'
body.should have_tag(:some_tag)
# NB! the custom URL must be declared in the MyTestApp in order to work
erb_app "<%= 'custom-erb-url'.upcase %>", :url => "/custom-erb-url"
last_request.path_info.should == "/custom-erb-url"
104 105 106 107 |
# File 'lib/sinatra/tests/test_case.rb', line 104 def erb_app(view, = {}) = {:layout => '<%= yield %>', :url => '/tests' }.merge() get [:url], :view => view, :layout => [:layout], :engine => :erb end |
#haml_app(view, options = {}) ⇒ Object
Flexible method to test the HAML output
Examples
haml_app "= some_method('value')"
body.should == 'some result'
body.should have_tag(:some_tag)
# NB! the custom URL must be declared in the MyTestApp in order to work
haml_app "= 'custom-haml-url'.upcase", :url => "/custom-haml-url"
last_request.path_info.should == "/custom-haml-url"
124 125 126 127 |
# File 'lib/sinatra/tests/test_case.rb', line 124 def haml_app(view, = {}) = {:layout => '= yield ', :url => '/tests' }.merge() get [:url], :view => view, :layout => [:layout], :engine => :haml end |
#mock_app(base = Sinatra::Base, &block) ⇒ Object
Sets up a Sinatra::Base subclass defined with the block given. Used in setup or individual spec methods to establish the application.
45 46 47 |
# File 'lib/sinatra/tests/test_case.rb', line 45 def mock_app(base=Sinatra::Base, &block) @app = Sinatra.new(base, &block) end |
#respond_to?(symbol, include_private = false) ⇒ Boolean
Also check response since we delegate there.
82 83 84 |
# File 'lib/sinatra/tests/test_case.rb', line 82 def respond_to?(symbol, include_private=false) super || (response && response.respond_to?(symbol, include_private)) end |
#setup ⇒ Object
Declaration of Sinatra setup
37 38 39 |
# File 'lib/sinatra/tests/test_case.rb', line 37 def setup Sinatra::Base.set :environment, :test end |
#status ⇒ Object
Short for last_response.status
. Making it easier to work with the returned status of a response
64 65 66 |
# File 'lib/sinatra/tests/test_case.rb', line 64 def status response.status end |