存根HTTP方请求以运行规范

时间:2018-07-07 16:19:26

标签: ruby-on-rails ruby rspec mocking

我需要对HTTP Party请求进行存根处理以运行我的规范,并且必须存储从parsed_response中获取的交易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"

1 个答案:

答案 0 :(得分:2)

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

function findIndexOfGreatest(array) {
   var greatest;
   var indexOfGreatest

   greatest = array.reduce((num1, num2) => {
		if(num1 > num2) {
			return num1
		} else {
			return num2
		}
   })

   indexOfGreatest = array.indexOf(greatest);
   return indexOfGreatest
}

// TEST
var array = [0, -1, 3, 6, 7, 4, 2, 8, 22, 11, -4, 7, 5, 3, 9]
var indexOfGreatest = findIndexOfGreatest(array)  

console.log(indexOfGreatest) // 8 (index of 22 in array)

它必须是有效的json对象,而不是ruby哈希。

这是另一种方式:

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