我正试图像这样嘲笑Stripe:
stub_request(:post, "https://api.stripe.com/v1/tokens").with(
:body => {"card" => {"number" => "4242424242424242", "exp_month"=> "12", "exp_year" => "2018", "cvc" => "123"}}
).
to_return(:status => 200, :headers => {}, :body => {
"id" => "tok_14CgP22eZvKYlo2CkgJ6ymLE",
"livemode" => false,
"created" => 1404517660,
"used" => false,
"object" => "token",
"type" => "card",
"card" => {
"id" => "card_14CgP22eZvKYlo2CcdUNvicW",
"object" => "card",
"last4" => "4242",
"brand" => "Visa",
"funding" => "credit",
"exp_month" => 12,
"exp_year" => 2018,
"fingerprint" => "Xt5EWLLDS7FJjR1c",
"country" => "US"
}
})
我打电话时收到no implicit conversion of Hash into String
错误:
token = Stripe::Token.create(:card => {
:number => params["number"],
:exp_month => params["expiry_month"],
:exp_year => params["expiry_year"],
:cvc => params["cvc"]})
这是因为body
中的to_return
中的哈希,但我不知道如何避免错误。
如果我向哈希添加.to_s
,我会得到:
来自API的无效响应对象:“{\”id \“=> \”tok_14CgP22eZvKYlo2CkgJ6ymLE \“,”livemode \“=> false,\”created \“=> 1404517660,\”used \“ => false,\“object \”=> \“token \”,\“type \”=> \“card \”,\“card \”=> {\“id \”=> \“card_14CgP22eZvKYlo2CcdUNvicW \”,\“object \”=> \“card \”,\“last4 \”=> \“4242 \”,\“brand \”=> \“Visa \”,\“ finance \“=> \”credit \“,\”exp_month \“=> 12,\”exp_year \“=> 2018,\”fingerprint \“=> \”Xt5EWLLDS7FJjR1c \“,\”country \ “=> \”US \“}}”(HTTP响应代码为200)
如果我在最后添加.to_json
,Stripe::Token
会通过,但当我尝试以任何方式使用生成的stack error too deep
时,我收到token
错误。
你推荐什么?
答案 0 :(得分:0)
可能是你的存根没有正确返回。它可能有助于使你的存根不那么具体。例如:
body = {
"id" => "tok_14CgP22eZvKYlo2CkgJ6ymLE",
"livemode" => false
# snip...
}
stub_request(:post, "https://api.stripe.com/v1/tokens")
.to_return(:status => 200, :body => body.to_json)
然而,很难确切地知道错误是什么,所以我只能提出可能有帮助的模糊想法。调用token
会给你带来什么错误?