Skip to content

Instantly share code, notes, and snippets.

@coderxin
Forked from rahsheen/rspec_mock_net_http.rb
Created August 19, 2020 15:49

Revisions

  1. @rahsheen rahsheen created this gist Nov 30, 2017.
    27 changes: 27 additions & 0 deletions rspec_mock_net_http.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    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