webmock icon indicating copy to clipboard operation
webmock copied to clipboard

[Proposal] Create matcher to match request params and headers

Open leomao10 opened this issue 4 years ago • 1 comments

The current matcher have_been_requested is quite awesome, however, I found that the code I endup with would be something like this:

    let(:email) { FFaker::Internet.email }
    let(:org_id) { SecureRandom.hex(24) }
    let(:url) { "#{ENV['THIRD_PARTY_HOST']}/authentication/resend_invite" }
    let!(:resend_invite_request) do
      stub_request(:post, url)
        .with(
          body: {
            email: email,
            orgId: org_id
          },
          headers: {"Authorization" => "Bearer #{registration_token}"}
        ).to_return(body: {}.to_json, status: 200)
    end
    
    before do
      method_make_api_call
    end

    it { expect(resend_invite_request).to have_been_requested }

But it is not really clear that what I want to test.

What I really care is, I would expect method_make_api_call would trigger api with certain params, so the ideal syntax would be:

    let(:email) { FFaker::Internet.email }
    let(:org_id) { SecureRandom.hex(24) }
    let(:url) { "#{ENV['THIRD_PARTY_HOST']}/authentication/resend_invite" }

    let!(:resend_invite_request) do
      stub_request(:post, url).to_return(body: {}.to_json, status: 200)
    end
    
    before do
      method_make_api_call
    end

    it "call resend_invite request with email and org_id" do 
      expect(resend_invite_request).to have_been_requested.with(
          body: {
            email: email,
            orgId: org_id
          },
          headers: {"Authorization" => "Bearer #{registration_token}"}
        )
    end

leomao10 avatar Jun 10 '20 07:06 leomao10

@leomao10 thank you for submitting this issue.

Why not to add the params to the request stub?

I like how your proposed code is readable. My concern would be not to confuse people about what is the 'right' way to use that matcher.

bblimke avatar Sep 10 '20 00:09 bblimke