Sumanth Madishetty
Sumanth Madishetty

Reputation: 3605

Stubbing a HTTP Party request to run Specs

I need to stub my HTTP Party request to run my spec and I have to store the transaction Id i get from the parsed_response.Here is my stub

stub_request(:post, {MYURL).to_return(status: 200, body: "{'Success': { 'TransactionId' => '123456789' }}", headers: {})

I get my response to the request as

#<HTTParty::Response:0x5d51240 parsed_response="{'Success': { 'TransactionId' => '123456789' }}", @response=#<Net::HTTPOK 200  readbody=true>, @headers={}>

i need to store transactionid from the field

response.parsed_response['Success']["perfiosTransactionId"]

by i am getting null from there.Can any one help me modify my stub response so that i could get the transactionid saved

PS: If I check the fileds of response i get

response.success? ----> true
response.parsed_response --> "{'Success': { 'TransactionId' => '123456789' }}"

response.parsed_response['Success']  ---> "Success"

Upvotes: 3

Views: 1503

Answers (1)

Roman Kiselenko
Roman Kiselenko

Reputation: 44380

You're sending the payload in wrong format:

stub_request(
  :post, 
  {MYURL}
).to_return(
  status: 200, 
  body: '{"Success": { "TransactionId": "123456789" }}', # valid json string
  headers: {"Content-Type" => "application/json"}
)

It's must be a valid json object, not a ruby hash.

Here is another way:

stub_request(
  :post, 
  {MYURL}
).to_return(
  status: 200, 
  body: {
    "Success": { "TransactionId" => "123456789" }
  }.to_json, # valid json string
  headers: {"Content-Type" => "application/json"}
)

Upvotes: 6

Related Questions