我试图在方法中存根方法的行为:
class A
def method_one(an_argument)
begin
external_obj = ExternalThing.new
result = external_obj.ext_method(an_argument)
rescue Exception => e
logger.info(e.message)
end
end
end
规格:
it "should raise an Exception when passed a bad argument" do
a = A.new
external_mock = mock('external_obj')
external_mock.stub(:ext_method).and_raise(Exception)
expect { a.method_one("bad") }.to raise_exception
end
但是,永远不会引发异常。
我也试过:
it "should raise an Exception when passed a bad argument" do
a = A.new
a.stub(:ext_method).and_raise(Exception)
expect { a.method_one("bad") }.to raise_exception
end
这也不起作用。在这种情况下,如何正确地存根外部方法以强制异常?
提前感谢您的任何想法!