我需要存根我的HTTP参与方请求以运行我的规范,并且我必须存储从解析的响应中获得的事务Id

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

我得到的答复是
#<HTTParty::Response:0x5d51240 parsed_response="{'Success': { 'TransactionId' => '123456789' }}", @response=#<Net::HTTPOK 200  readbody=true>, @headers={}>

我需要存储来自字段的transactionID
response.parsed_response['Success']["perfiosTransactionId"]

我从那里得到了空值。有谁能帮我修改存根响应以便我可以保存transactionid
PS:如果我检查回复的文件
response.success? ----> true
response.parsed_response --> "{'Success': { 'TransactionId' => '123456789' }}"

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

最佳答案

您发送的有效负载格式错误:

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

它必须是有效的json对象,而不是ruby散列。
这是另一种方式:
stub_request(
  :post,
  {MYURL}
).to_return(
  status: 200,
  body: {
    "Success": { "TransactionId" => "123456789" }
  }.to_json, # valid json string
  headers: {"Content-Type" => "application/json"}
)

关于ruby-on-rails - stub HTTP方请求以运行规范,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51224912/

10-12 00:37