在使用WebMock模拟Stripe时,为什么我得到“没有将Hash隐式转换为字符串”

时间:2014-07-06 09:24:17

标签: ruby ruby-on-rails-4 rspec stripe-payments webmock

我正试图像这样嘲笑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_jsonStripe::Token会通过,但当我尝试以任何方式使用生成的stack error too deep时,我收到token错误。

你推荐什么?

1 个答案:

答案 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会给你带来什么错误?