摩卡存根有异常 - rspec 测试未通过
嘿, 我正在尝试使用 Mocha 和 Rspec 来测试方法总是引发一些异常的场景。
这是我试图测试的控制器代码:
def add_parent
begin
parent = Entity.find_by_id(params[:parent_id])
if !parent.nil?
@entity.add_parent!(parent)
flash[:success] = "Entity successfully updated."
else
raise "Parent does not exist."
end
rescue
flash[:error] = "Something bad happened. #{$!}"
end
redirect_to @entity
end
这是测试代码:
it "should flash error if exception is thrown when adding parent" do
Entity.any_instance.stubs(:add_parent!).raises(Exception)
lambda do
post :add_parent, :id => @entity[:id],
:parent_id => @parent_entity[:id]
end.should_not change(@entity.parents, :count)
flash[:error].should =~ /something bad happened/i
end
这是正在被存根的方法:
def add_parent!(parent)
Entity.transaction do
lock!('lock in share mode')
self.parents << parent
end
end
我收到以下 rspec 错误,该错误信息非常少,所以我不知道如何解决它。
Failures:
1) EntitiesController POST 'add_parent' for signed-in users allow access with edit permission should flash error if exception is thrown when adding parent
Failure/Error: post :add_parent, :id => @entity[:id],
Exception
# ./app/controllers/entities_controller.rb:81:in `add_parent'
# ./spec/controllers/entities_controller_spec.rb:1010
Hey,
I am trying to use Mocha and Rspec to test a scenario where a method always raises some exception.
Here is the controller code I am trying to test:
def add_parent
begin
parent = Entity.find_by_id(params[:parent_id])
if !parent.nil?
@entity.add_parent!(parent)
flash[:success] = "Entity successfully updated."
else
raise "Parent does not exist."
end
rescue
flash[:error] = "Something bad happened. #{$!}"
end
redirect_to @entity
end
Here is the test code:
it "should flash error if exception is thrown when adding parent" do
Entity.any_instance.stubs(:add_parent!).raises(Exception)
lambda do
post :add_parent, :id => @entity[:id],
:parent_id => @parent_entity[:id]
end.should_not change(@entity.parents, :count)
flash[:error].should =~ /something bad happened/i
end
Here is the method which is being stubbed:
def add_parent!(parent)
Entity.transaction do
lock!('lock in share mode')
self.parents << parent
end
end
I am getting the following rspec error, which is pretty uninformative so I don't know how to resolve it..
Failures:
1) EntitiesController POST 'add_parent' for signed-in users allow access with edit permission should flash error if exception is thrown when adding parent
Failure/Error: post :add_parent, :id => @entity[:id],
Exception
# ./app/controllers/entities_controller.rb:81:in `add_parent'
# ./spec/controllers/entities_controller_spec.rb:1010
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
哇哦,首先,从一大块代码中拯救而不给出您在这里期望的异常类是一个非常坏的习惯。拯救一切是没有好处的。最好删除整个救援,也许使用
find
而不是find_by_id
,这样 Rails 就可以捕获 404 错误,而你就不用担心了。另一方面,这个错误应该经常发生吗?看起来像一些数据库的东西正在发生,所以我不希望它失败。其次,我认为你必须像手册中所说的那样进行测试
raise_error
http://relishapp .com/rspec/rspec-expectations/v/2-6/dir/built-in-matchers/raise-error-matcher
Wooah, first of all, it's a very bad habit to rescue from a big chunk of code without give the exception class you are expecting here. rescueing everything is no good. Best remove the whole rescueing, maybe use
find
overfind_by_id
so rails can catch that 404 error and you aren't bothered about. On the other hand, is this error supposed to happen a lot? Looks like some db-stuff going on so I wouldn't expect it to fail.Secondly, I think you must test like the manual says about
raise_error
http://relishapp.com/rspec/rspec-expectations/v/2-6/dir/built-in-matchers/raise-error-matcher
不带参数的
rescue
将拯救 StandardError 及其后代。如果您想捕获所有异常,则应该rescue Exception
,因为所有异常都源自 Exception 类。来源:http://www.ruby-forum.com/topic/44495
rescue
with no argument will rescue StandardError and its descendants. If you want to catch ALL exceptions, you shouldrescue Exception
as all exceptions descend from class Exception.Source: http://www.ruby-forum.com/topic/44495