Sum*_*tty 3 ruby rspec ruby-on-rails mocking
我需要存根我的 HTTP 方请求来运行我的规范,并且我必须存储从 parsed_response 获得的事务 ID。这是我的存根
stub_request(:post, {MYURL).to_return(status: 200, body: "{'Success': { 'TransactionId' => '123456789' }}", headers: {})
Run Code Online (Sandbox Code Playgroud)
我得到对请求的答复
#<HTTParty::Response:0x5d51240 parsed_response="{'Success': { 'TransactionId' => '123456789' }}", @response=#<Net::HTTPOK 200 readbody=true>, @headers={}>
Run Code Online (Sandbox Code Playgroud)
我需要存储来自现场的 transactionid
response.parsed_response['Success']["perfiosTransactionId"]
Run Code Online (Sandbox Code Playgroud)
我从那里得到 null。任何人都可以帮我修改我的存根响应,以便我可以保存 transactionid
PS:如果我检查我得到的回复文件
response.success? ----> true
response.parsed_response --> "{'Success': { 'TransactionId' => '123456789' }}"
response.parsed_response['Success'] ---> "Success"
Run Code Online (Sandbox Code Playgroud)
您以错误的格式发送有效负载:
stub_request(
:post,
{MYURL}
).to_return(
status: 200,
body: '{"Success": { "TransactionId": "123456789" }}', # valid json string
headers: {"Content-Type" => "application/json"}
)
Run Code Online (Sandbox Code Playgroud)
它必须是有效的 json 对象,而不是 ruby 哈希。
这是另一种方法:
stub_request(
:post,
{MYURL}
).to_return(
status: 200,
body: {
"Success": { "TransactionId" => "123456789" }
}.to_json, # valid json string
headers: {"Content-Type" => "application/json"}
)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1429 次 |
| 最近记录: |