Testing

Keep your test suite off the network by swapping the transport, and assert on what would have been sent.

The transport is anything that responds to call(request) and returns a Blackevin::Response. Set one in the configuration and no Blackevin::Rest built afterwards leaves the process:

spec/support/blackevin.rb
RSpec.configure do |config|
  config.before do
    Blackevin.configure do |blackevin|
      blackevin.key = 'secret.test'
      blackevin.transport = ->(_request) { Blackevin::Response.new(status: 200, body: '{}') }
    end
  end

  config.after { Blackevin.reset! }
end

Signing a token request never touched the network in the first place, so a controller spec for your token endpoint needs only the key.

Asserting on a publish

Record the requests instead of discarding them:

class RecordingTransport
  attr_reader :requests

  def initialize = @requests = []

  def call(request)
    @requests << request

    Blackevin::Response.new(status: 200, body: '{}')
  end
end
it 'announces the shipment' do
  transport = RecordingTransport.new

  Blackevin.configure { _1.transport = transport }

  ShipOrder.call(order)

  request = transport.requests.last

  expect(request.method).to eq('POST')
  expect(request.url).to end_with("/api/channels/orders%3A#{order.id}/publish")
  expect(JSON.parse(request.body)).to include('name' => 'status')
end

A Blackevin::Request carries method, url, headers and body.

Simulating a refusal

Blackevin.configure do |config|
  config.transport = lambda do |_request|
    Blackevin::Response.new(status: 429, body: '{"error":"queue limit reached","reason":"queue_limit"}')
  end
end

The gem raises the same Blackevin::Error it would against the real node, so the rescue in your code is exercised for real.

On this page