ruby-on-rails - stub rspec and_raise 并添加一条消息

标签 ruby-on-rails rspec coinbase-api

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

型号代码:

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/

相关文章:

ruby-on-rails - 用于构建社交网站的 Ruby 电子书

ruby - Allow vs Stub,有什么区别?

javascript - Jquery ajax 调用返回 URL 并加载到框架或 div 中

php - 我如何与 Coinbase 的 API 交互?为什么总是失败?

node.js - Coinbase API client.getAccount(...) 开始返回 : "Error: unable to get local issuer certificate"

ruby-on-rails - 如何使用 Heroku 和 Cloudfront 为 Rails 激活页面缓存?

ruby-on-rails - rails 错误?除非 parent 修改(嵌套形式),否则孙子不保存

javascript - 绕过 XHR 的 CSRF 保护是否安全? ( rails )

ruby-on-rails - 使用rspec验证模型时测试错误消息

ruby-on-rails - 为什么我的response.body 是空的