我正在编写需要在我的代码中测试救援的测试。
型号代码:
rescue Coinbase::Error => e
#debugger
if e == "You don't have that many bitcoins in your account to sell."
...
end
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.")
在我所做的地方添加调试器并查看
e
的值在控制台中,我看到#<Coinbase::UnauthorizedError: Coinbase::UnauthorizedError>
所以消息没有被传递。
在过去的 40 分钟里,我一直在用谷歌搜索这个,我发现的一切只包括发送错误类,而不是消息。据推测,在某些情况下,错误类别相同但消息不同。
任何建议都会很棒。谢谢!
最佳答案
我想你想做:Coinbase::Error.new("You don't have that many bitcoins in your account to sell.")
在加注中。
更新,我想你也想要 e.message == ""
不是 e == ""
因为您将错误与字符串进行比较,而不是错误消息。
关于ruby-on-rails - stub rspec and_raise 并添加一条消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26858262/