Body of the stub_request with regex
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.
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
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(...)?
Could you please send the example that is not working?
stub_request(:post, "url"). with{ |request| request.body == {"name" => "John Doe","email" => /.@./ ,"phone_number" => /^[0-9]*$/, "linkedin_profile" => ""} }. to_return(status: 200, body: "", headers: {})