-
-
Save coderxin/9a02b618738167f6fc72dc0ed7f8f3dc to your computer and use it in GitHub Desktop.
Easily Mock Net::HTTP with Rspec
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rails_helper' | |
RSpec.describe MyWorker, type: :job do | |
include ActiveJob::TestHelper | |
let(:sqs_msg) { double AWS::SQS::ReceivedMessage, | |
id: 'fc754df7-9cc2-4c41-96ca-5996a44b771e', | |
body: 'test', | |
delete: nil } | |
describe '#perform' do | |
subject(:job) { described_class.new } | |
it 'sends the message' do | |
# Setup successful response | |
response = Net::HTTPSuccess.new(1.0, '200', 'OK') | |
# Setup Net::HTTP to receive the response | |
expect_any_instance_of(Net::HTTP).to receive(:request) { response } | |
# Setup response to receive follow messages below. | |
# This prevents the elusive "undefined method `close' for nil:NilClass" error. | |
expect(response).to receive(:body) { '{"data": "that you want to return"}' } | |
job.perform(sqs_msg) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment