Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.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 检查参数错误文本_Ruby_Rspec - Fatal编程技术网

Ruby 检查参数错误文本

Ruby 检查参数错误文本,ruby,rspec,Ruby,Rspec,使用rspec如何检查参数的错误文本 我有一个方法,如果你给它加一个零,就会产生一个ArgumentError if amount_paid <= 0 raise ArgumentError, 'Please insert Money' end 这个测试通过了。当我为该方法提供零时,将引发参数错误。但是,如何检查参数错误的文本?我的代码中有不同的参数错误,我希望确保它是正确的 谢谢您已经介绍过了。他们的例子是: expect { raise StandardError, 'this

使用rspec如何检查参数的错误文本

我有一个方法,如果你给它加一个零,就会产生一个ArgumentError

if amount_paid <= 0
   raise ArgumentError, 'Please insert Money'
end
这个测试通过了。当我为该方法提供零时,将引发参数错误。但是,如何检查参数错误的文本?我的代码中有不同的参数错误,我希望确保它是正确的

谢谢

您已经介绍过了。他们的例子是:

expect { raise StandardError, 'this message exactly' }.to raise_error('this message exactly')

谢谢你,马克。因此,我将代码改为:
@method。检查\u money(“酷可乐”,“0.00”)。除了{raise ArgumentError,'Please insert money'}。要引起\u error('Please insert money')
但是现在我在运行测试时收到一条错误消息-
ArgumentError:Please insert money./money.rb:19:in money./spec/money\u spec.rb:17:in block(2级)在“
”中,我也尝试使用标准错误,但问题相同。你知道这是什么原因吗?当我使用lambda时效果很好。我怀疑您可能希望显式命名异常类:
…以引发错误(ArgumentError,'Please insert Money'
是的,已将其排序。使用以下
expect@方法。检查\u Money(“可口可乐”,“0.00”)}。以引发错误(“Please insert Money”)
expect { raise StandardError, 'this message exactly' }.to raise_error('this message exactly')