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

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

#bodyObject

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, options = {})
  options = {:layout => '<%= yield %>', :url  => '/tests' }.merge(options)
  get options[:url], :view => view, :layout => options[: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, options = {}) 
  options = {:layout => '= yield ', :url  => '/tests' }.merge(options)
   get options[:url], :view => view, :layout => options[: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.

Returns:

  • (Boolean)


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

#setupObject

Declaration of Sinatra setup



37
38
39
# File 'lib/sinatra/tests/test_case.rb', line 37

def setup
  Sinatra::Base.set :environment, :test
end

#statusObject

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