Ruby-on-rails 如何使用 RSpec 的 should_raise 有任何异常?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1722749/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 21:56:55  来源:igfitidea点击:

How to use RSpec's should_raise with any kind of exception?

ruby-on-railsrubyexception-handlingrspec

提问by marcgg

I'd like to do something like this:

我想做这样的事情:

some_method.should_raise <any kind of exception, I don't care>

How should I do this?

我该怎么做?

some_method.should_raise exception

... doesn't work.

...不起作用。

回答by Avdi

expect { some_method }.to raise_error

RSpec 1 Syntax:

RSpec 1 语法:

lambda { some_method }.should raise_error

See the documentation(for RSpec 1 syntax) and RSpec 2 documentationfor more.

有关更多信息,请参阅文档(有关 RSpec 1 语法)和RSpec 2 文档

回答by joelparkerhenderson

RSpec 2

规格 2

expect { some_method }.to raise_error
expect { some_method }.to raise_error(SomeError)
expect { some_method }.to raise_error("oops")
expect { some_method }.to raise_error(/oops/)
expect { some_method }.to raise_error(SomeError, "oops")
expect { some_method }.to raise_error(SomeError, /oops/)
expect { some_method }.to raise_error(...){|e| expect(e.data).to eq "oops" }

# Rspec also offers to_not:
expect { some_method }.to_not raise_error
...

Note: raise_errorand raise_exceptionare interchangeable.

注:raise_errorraise_exception可以互换。

RSpec 1

规格 1

lambda { some_method }.should raise_error
lambda { some_method }.should raise_error(SomeError)
lambda { some_method }.should raise_error(SomeError, "oops")
lambda { some_method }.should raise_error(SomeError, /oops/)
lambda { some_method }.should raise_error(...){|e| e.data.should == "oops" }

# Rspec also offers should_not:
lambda { some_method }.should_not raise_error
...

Note: raise_erroris an alias for raise_exception.

注意:raise_error是 的别名raise_exception

Documentation: https://www.relishapp.com/rspec

文档:https: //www.relishapp.com/rspec

RSpec 2:

规范 2:

RSpec 1:

规范 1:

回答by racc

Instead of lambda, use expect to:

使用 expect 代替 lambda:

   expect { some_method }.to raise_error

This is applies for more recent versions of rspec, i.e. rspec 2.0 and up.

这适用于更新版本的 rspec,即 rspec 2.0 及更高版本。

See the docofor more.

更多信息请参见doco

回答by ayckoster

The syntax changed recently and now it is:

语法最近发生了变化,现在是:

expect { ... }.to raise_error(ErrorClass)

回答by Bruno E.

From version 3.3 on rspec-expectionsgem raises a warning for a blank raise_error without a parameter

从版本 3.3 开始,rspec-expectionsgem 会针对没有参数的空白 raise_error 发出警告

expect { raise StandardError }.to raise_error # results in warning
expect { raise StandardError }.to raise_error(StandardError) # fine

This gives you a hint that your code may fail with a different error than the test intended to check.

这给你一个提示,你的代码可能会因为与测试要检查的错误不同的错误而失败。

WARNING: Using the raise_errormatcher without providing a specific error or message risks false positives, since raise_errorwill match when Ruby raises a NoMethodError, NameErroror ArgumentError, potentially allowing the expectation to pass without even executing the method you are intending to call. Instead consider providing a specific error class or message. This message can be supressed by setting: RSpec::Expectations.configuration.warn_about_potential_false_positives = false.

警告:在raise_error不提供特定错误或消息的情况下使用匹配器有误报的风险,因为raise_error当 Ruby 引发NoMethodError, NameErroror时将匹配ArgumentError,可能允许期望通过甚至不执行您打算调用的方法。而是考虑提供特定的错误类或消息。可以通过设置来抑制此消息:RSpec::Expectations.configuration.warn_about_potential_false_positives = false