Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 为什么我在用WebMock模拟Stripe时会得到这种“哈希到字符串的非隐式转换”_Ruby_Ruby On Rails 4_Rspec_Stripe Payments_Webmock - Fatal编程技术网

Ruby 为什么我在用WebMock模拟Stripe时会得到这种“哈希到字符串的非隐式转换”

Ruby 为什么我在用WebMock模拟Stripe时会得到这种“哈希到字符串的非隐式转换”,ruby,ruby-on-rails-4,rspec,stripe-payments,webmock,Ruby,Ruby On Rails 4,Rspec,Stripe Payments,Webmock,我试着这样模仿条纹: 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 =>

我试着这样模仿条纹:

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"
  }
})
调用以下命令时,我得到了哈希到字符串的非隐式转换错误:

token = Stripe::Token.create(:card => {
  :number => params["number"],
  :exp_month => params["expiry_month"],
  :exp_year => params["expiry_year"],
  :cvc => params["cvc"]})
这是因为to_返回中的body中存在散列,但我不知道如何避免错误

如果我将.to_添加到哈希中,我会得到:

来自API的无效响应对象:{\id\=>\tok\U 14CGP22Evkylo2CKGJ6YMLE\,\livemode\=>false\created\=>1404517660\used\=>false\object\=>\token\,\type\=>\card\,\card\=>{\id\=>\card\cgp22ezvkylo2cdcdunicw\,\object\=>\card\,\last4\=>\4242\,\brand\=>\Visa\,\funding\=>\credit\,\exp\u month\=>12、\exp\u year\=>2018、\Xt5EWLLDS7FJjR1c\,\country\=>\US\}HTTP响应代码为200

如果我在末尾添加一个.to_json,Stripe::Token会通过,但是当我试图以任何方式使用生成的Token时,我会得到一个堆栈错误太深的错误


您有什么建议?

可能是您的存根没有正确返回。这可能有助于降低存根的具体程度。例如:

body = {
  "id" => "tok_14CgP22eZvKYlo2CkgJ6ymLE",
  "livemode" => false
  # snip...
}

stub_request(:post, "https://api.stripe.com/v1/tokens")
  .to_return(:status => 200, :body => body.to_json)

然而,很难确切地知道抛出了什么错误,所以我只能给出一些模糊的想法,这可能会有所帮助。调用令牌会给您带来什么错误?

哪段代码从API返回无效的响应对象?:存根请求中的主体需要是字符串。to_json听起来是正确的做法,但它会导致什么错误e?错误来自token=Stripe::token.create:card=>{:number=>params[number],:exp_month=>params[expiry_month],:exp_year=>params[expiry_year],:cvc=>params[cvc]}.With.to_json,Stripe::Token没有抛出错误,但调用生成的Token会抛出错误。不幸的是,它给出的堆栈错误太深。即使更改为mocking,您是否可以用其余代码更新该问题?