Using Rack::Test to test the entire app stack as defined in config.ru

Language: Ruby

# Rack::Test requires you to return an app object from the #app method in your test case.
# If you want to test one app in isolation, you just return that app, but if you want
# to test the entire app stack, including middlewares, cascades etc. you need to parse
# the app defined in config.ru.
#
# With the latest Rack version 1.1.0, you can use Rack::Builder.parse_file to return the
# outer app; with older Rack versions, you'll need to do the evaling yourself, like so:
#
#   OUTER_APP = eval("Rack::Builder.new {( " + ::File.read('config.ru') + "\n )}.to_app")

OUTER_APP = Rack::Builder.parse_file('config.ru').first

class TestApp < Test::Unit::TestCase
  include Rack::Test::Methods
  
  def app
    OUTER_APP
  end
  
  def test_root
    get '/'
    assert last_response.ok?
  end
end
Reveal More
Added over 2 years ago by Devo_normal gbuesing