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:
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! }
endSigning 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
endit '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')
endA 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
endThe gem raises the same Blackevin::Error it would against the real node, so the
rescue in your code is exercised for real.