How to configure RSpec with Sinatra to dynamically determine which Sinatra application is running before running my test suite?

Good. I want to do query specifications with RSpec for my Sinatra application.

I have config.ru

# config.ru
require File.dirname(__FILE__) + '/config/boot.rb'

map 'this_route' do
  run ThisApp
end

map 'that_route' do
  run ThatApp
end

Boot.rb just uses the Bundler and requires additional applications for the rest of the application:

ThisApp looks like this:

# lib/this_app.rb

class ThisApp < Sinatra::Base
  get '/hello' do
    'hello'
  end
end

So, I use RSpec, and I want to write query specifications, for example:

# spec/requests/this_spec.rb
require_relative '../spec_helper'

describe "This" do
  describe "GET /this_route/hello" do
    it "should reach a page" do
      get "/hello"
        last_response.status.should be(200)
      end
    end

    it "should reach a page that says hello" do
      get "/hello"
        last_response.body.should have_content('hello')
      end
    end
  end
end

This works fine because my spec_helper.rb is configured as follows:

# spec/spec_helper.rb
ENV['RACK_ENV'] = "test"
require File.expand_path(File.dirname(__FILE__) + "/../config/boot")
require 'capybara/rspec'

RSpec.configure do |config|
  config.include Rack::Test::Methods
end

def app
  ThisApp
end

But my problem is that I want to test "ThatApp" from my file as a screenshot along with any number of additional applications that I could add later with "ThisApp". For example, if I had a second query specification file:

# spec/requests/that_spec.rb
require_relative '../spec_helper'

describe "That" do
  describe "GET /that_route/hello" do
    it "should reach a page" do
      get "/hello"
        last_response.status.should be(200)
      end
    end

    it "should reach a page that says hello" do
      get "/hello"
        last_response.body.should have_content('hello')
      end
    end
  end
end

RackTest , , , spec_helper "app", , Capybara.app , .

, - , , RackTest Capybara , . , , RSpec.configure, , , .

- , , - ? .

+3
2

sinatra, , app, . include MySinatraAppHelper , .

rspec, .

+1
0

All Articles