我需要存根我的HTTP Party请求来运行我的规范,并且我必须存储我从parsed_response.Here获得的事务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"]由于我从there.Can获得了null,任何人都可以帮助我修改存根响应,这样我就可以保存事务i
PS:如果我检查我得到的回复文件
response.success? ----> true
response.parsed_response --> "{'Success': { 'TransactionId' => '123456789' }}"
response.parsed_response['Success'] ---> "Success"发布于 2018-07-08 00:24:42
您正在以错误的格式发送负载:
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"}
)https://stackoverflow.com/questions/51224912
复制相似问题