ruby – RSpec,隐式主题和异常

ruby – RSpec,隐式主题和异常,第1张

概述有没有办法在rspec中使用隐式主题正确测试异常提升? 例如,这失败了: describe 'test' do subject {raise 'an exception'} it {should raise_exception}end 但这传递了: describe 'test' do it "should raise an exception" do lambda{rais 有没有办法在rspec中使用隐式主题正确测试异常提升?

例如,这失败了:

describe 'test' do  subject {raise 'an exception'}  it {should raise_exception}end

但这传递了:

describe 'test' do  it "should raise an exception" do    lambda{raise 'an exception'}.should raise_exception  endend

为什么是这样?

解决方法 subject接受一个返回剩余主题的块.

你想要的是这个:

describe 'test' do  subject { lambda { raise 'an exception' } }  it { should raise_exception }end

编辑:评论澄清

这个:

describe 'test' do   subject { foo }   it { should blah_blah_blah }end

或多或少相当于

(foo).should blah_blah_blah

现在,考虑:没有lambda,这变成:

(raise 'an exception').should raise_exception

在这里看到,在评估主题时会引发异常(之前应该被调用).而对于lambda,它变成:

lambda { raise 'an exception' }.should raise_exception

这里,主题是lambda,仅在评估should调用时(在将捕获异常的上下文中)进行评估.

虽然每次都会重新评估“主题”,但它仍然需要评估您想要调用的内容.

总结

以上是内存溢出为你收集整理的ruby – RSpec,隐式主题和异常全部内容,希望文章能够帮你解决ruby – RSpec,隐式主题和异常所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1286227.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-09
下一篇2022-06-09

发表评论

登录后才能评论

评论列表(0条)

    保存