Ad-hoc Rack Test Servers for Integration Tests

Boots up test servers so integration tests can connect to them (when running in the same process try webmock) … works for RSpec and Minitest (with minitest-around/maxitest or similar gem)

The server boot takes about 5 seconds, might be better with a different web-server, but WEBrick is simplest.

Code from this post is now the stub_server gem.

require 'stub_server'

describe 'CLI' do
  let(:service_a_replies) {{'/v1/catalog/services' => {}}}
  let(:service_b_replies) {{'/api/v1/nodes' => {}}}

  around do |test|
    StubServer.open(8500, service_a_replies) do |a|
      StubServer.open(9000, service_b_replies) do |b|
        a.wait
        b.wait
        test.call
      end
    end
  end

  it "works" do
    ...
  end
end

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s