Skip to content

Instantly share code, notes, and snippets.

@rahsheen
Created November 30, 2017 19:33
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save rahsheen/b428cd24f4f4b1141951d9f8f23a972f to your computer and use it in GitHub Desktop.
Save rahsheen/b428cd24f4f4b1141951d9f8f23a972f to your computer and use it in GitHub Desktop.
Easily Mock Net::HTTP with Rspec
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
@ValterAndrei
Copy link

You save my life hehe

@rahsheen
Copy link
Author

You save my life hehe

Happy to help👍

@conrc
Copy link

conrc commented Jan 13, 2022

Very helpful, thanks.

@yashka713
Copy link

❤️

@TecnoSigma
Copy link

Very good! tks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment