ruby – RSpec,隐式主题和异常
发布时间:2020-12-17 02:52:50 所属栏目:百科 来源:网络整理
导读:有没有办法在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
有没有办法在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 end end 为什么是这样? 解决方法
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调用时(在将捕获异常的上下文中)进行评估. 虽然每次都会重新评估“主题”,但它仍然需要评估您想要调用的内容. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |