我需要对我的 HTTP Party 请求 stub 以运行我的规范,并且我必须存储从 parsed_response 获得的事务 ID。这是我的 stub
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={}>
我需要从字段中存储交易ID
response.parsed_response['Success']["perfiosTransactionId"]
我从那里得到空值。任何人都可以帮我修改我的 stub 响应,以便我可以保存事务 ID
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 Party 请求以运行 Specs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51224912/