awsm sid
awsm sid

Reputation: 595

Stub request when request body is different

Hi I am working on RoR project with ruby-2.5.0 and Rails 5. I am stubbing an http request with stub_request. This http request is basically a mailjet api request to send email.

So, I can not just reuse the snippet, rspec spits out to me, because body differs on every execution.

Stub request

stub_request(:post, "https://api.mailjet.com/v3/send").
           with(
             body: "{\"FromEmail\":\"[email protected]\",\"FromName\":\"abc\",\"To\":\"[email protected]\",\"Subject\":\"Forgot Password\",\"Text-part\":\"xV3SEtaoa1oyYYfYBt12pw\"}",
             headers: {
            'Accept'=>'application/json',
            'Accept-Encoding'=>'deflate',
            'Authorization'=>'Basic YmY0NzdhZmYyMmZlMGQwMzAxNzYzNDNkODUwZTY1MzE6YmRhNmZmYzQ0Y2ZmNGM3MmZkZGNkMjUwODA5YWJhZjM=',
            'Content-Length'=>'143',
            'Content-Type'=>'application/json',
            'Host'=>'api.mailjet.com',
            'User-Agent'=>'mailjet-api-v3-ruby/1.5.4'
             }).
           to_return(status: 200, body: "", headers: {})

Here Text-part is different for each user. I tried hash_including method as follows:-

stub_request(:post, "https://api.mailjet.com/v3/send").
           with(
             body: hash_including({"FromEmail": "[email protected]","FromName": "abc"}),
             headers: {
            'Accept'=>'application/json',
            'Accept-Encoding'=>'deflate',
            'Authorization'=>'Basic YmY0NzdhZmYyMmZlMGQwMzAxNzYzNDNkODUwZTY1MzE6YmRhNmZmYzQ0Y2ZmNGM3MmZkZGNkMjUwODA5YWJhZjM=',
            'Content-Length'=>'143',
            'Content-Type'=>'application/json',
            'Host'=>'api.mailjet.com',
            'User-Agent'=>'mailjet-api-v3-ruby/1.5.4'
             }).
           to_return(status: 200, body: "", headers: {})

But when i run test it throws an error:-

JSON::ParserError:
       765 : unexpected token at ''

Please help me what is the right syntax to use hash_including method. Thanks in advance.

Upvotes: 2

Views: 1642

Answers (0)

Related Questions