webmock icon indicating copy to clipboard operation
webmock copied to clipboard

Body of the stub_request with regex

Open FMMFHD opened this issue 6 years ago • 4 comments

I m programming with ruby 2.5.1 and I'm using a Faker to randomly choose a random email. This email is going to be sent via API.

stub_request(...). with( body: {"email" => /.@./} )

But this error appears: [["~", "email", "[email protected]", /.@./]

I think that the regex matcher isn't working.

FMMFHD avatar Aug 21 '19 18:08 FMMFHD

There is no regex matcher therefore it can't work. It's just a hash with regex value.

You can consider using a block to match request:

https://github.com/bblimke/webmock#matching-requests-against-provided-block

Or you can match the whole body against regex: https://github.com/bblimke/webmock#setting-expectations-in-rspec-on-webmock-module

bblimke avatar Aug 21 '19 18:08 bblimke

I try to use a block o match request but the program assumes that the stub_request don't have the field 'with'.

I have two fields that I test using Faker so every time I run the rspec the values are different.

How can I write a stub_request(:post,url).with(...).to_return(...)?

FMMFHD avatar Aug 22 '19 11:08 FMMFHD

Could you please send the example that is not working?

bblimke avatar Aug 22 '19 11:08 bblimke

stub_request(:post, "url"). with{ |request| request.body == {"name" => "John Doe","email" => /.@./ ,"phone_number" => /^[0-9]*$/, "linkedin_profile" => ""} }. to_return(status: 200, body: "", headers: {})

FMMFHD avatar Aug 22 '19 12:08 FMMFHD