0
我有一个记录错误的方法。它将一个错误消息作为一个参数,但现在它需要一个完整的错误并在其上调用.backtrace。方法如下:在方法调用时模仿Rspec中的错误消息.backtrace
def log_error(error)
puts error.backtrace
puts "RECONCILE_TAX_RATES [#{Time.now}] ERROR [#{error.message}]"
end
我想测试它,我无法弄清楚测试的语法。我收到的是:
it 'logs errors' do
time = "Tue, 16 Sep 2014 20:18:19 UTC +00:00"
Timecop.freeze(time) do
tax_reconciler = TaxReconciler.new
error_message = "I'm sorry, Dave. I'm afraid I can't do that."
expected = "RECONCILE_TAX_RATES [2014-09-16 20:18:19 UTC] ERROR [I'm sorry, Dave. I'm afraid I can't do that.]"
STDOUT.should_receive(:puts).with(expected)
tax_reconciler.log_error(error_message)
end
end
我从RSPEC文档尝试各种组合,但我不断收到对.backtrace方法绊倒了。我怎样才能嘲笑这个错误消息,使.backtrace不会炸毁?在此先感谢您的帮助,并告知我是否需要提供任何信息。
编辑:对于类似的问题我用的解决方案是任何人:
it 'logs errors' do
time = "Tue, 16 Sep 2014 20:18:19 UTC +00:00"
Timecop.freeze(time) do
expected = "RECONCILE_TAX_RATES [2014-09-16 20:18:19 UTC] ERROR [I'm sorry, Dave. I'm afraid I can't do that.]"
tax_reconciler = TaxReconciler.new
begin
raise "I'm sorry, Dave. I'm afraid I can't do that."
rescue => error_message
STDOUT.should_receive(:puts).with(expected)
STDOUT.should_receive(:puts).with(error_message.backtrace)
tax_reconciler.log_error(error_message)
end
end
end
当我尝试这样我得到:未定义的方法'允许'for# –
2014-09-23 22:25:26
'allow'方法附带'rspec-mocks' gem。请参阅:https://github.com/rspec/rspec-mocks – spickermann 2014-09-24 00:52:34
我有rspec-mocks在应用程序,但旧版本(2.12.2。)。这个旧版本是否支持语法?该应用程序非常大,所以如果3.1.1不向后兼容,我不想更新和打破所有其他测试。 – 2014-09-24 16:31:05