存根 rspec and_raise 并添加消息

Zac*_*iro 5 rspec ruby-on-rails coinbase-api

我正在编写需要测试代码中的救援的测试。

型号代码:

rescue Coinbase::Error => e
  #debugger
  if e == "You don't have that many bitcoins in your account to sell."
  ...
end
Run Code Online (Sandbox Code Playgroud)

R规格代码:

allow_any_instance_of(Order).to receive(:sell).and_raise(Coinbase::Error, "You don't have that many bitcoins in your account to sell.")
Run Code Online (Sandbox Code Playgroud)

e在我所做的地方添加调试器并查看控制台中的值,我明白了

#<Coinbase::UnauthorizedError: Coinbase::UnauthorizedError>

所以消息没有被传入。

在过去的 40 分钟里,我一直在谷歌上搜索这个问题,我发现的所有内容都只涉及发送错误类,而不是消息。据推测,在某些情况下,错误类别相同,但消息不同。

任何建议都会很棒。谢谢!

Nat*_*and 2

我想你想做的是:Coinbase::Error.new("You don't have that many bitcoins in your account to sell.")在加薪电话中。

更新,我认为您也不想e.message == ""这样做e == "",因为您正在将错误与字符串而不是错误消息进行比较。