如何使用 rspec 测试构造函数中的方法调用
我有一个像这样的构造函数:
class Foo
def initialize(options)
@options = options
initialize_some_other_stuff
end
end
并且想要测试对 initialize_some_other_stuff
的调用(如果实例化一个新的 Foo 对象)。
我发现这个问题rspec:如何存根实例方法由构造函数调用? 但建议的调用 Foo.any_instance(:initialize_some_other_stuff)
的解决方案在我的 rspec 版本 (2.5.0) 中不起作用。
谁能帮我测试这个构造函数调用?
i have a constructor like this:
class Foo
def initialize(options)
@options = options
initialize_some_other_stuff
end
end
and want to test the call to initialize_some_other_stuff
if a instantiate a new Foo object.
I found this question rspec: How to stub an instance method called by constructor? but the suggested solution to call Foo.any_instance(:initialize_some_other_stuff)
does not work in my rspec version (2.5.0).
Can anyone help me to test this constructor call?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在您的规范中,您可以具有以下内容:
如果构造函数调用
initiaize_some_other_stuff
方法,则foo.initializer_known
应该为 true。In you spec, you could have the following:
If the constructor calls the
initiaize_some_other_stuff
method,foo.initializer_called
should be true.在这里:
stub_model(Foo).should_receive(:some_method_call).with(optional_argument)
Here you go:
stub_model(Foo).should_receive(:some_method_call).with(optional_argument)
由于
initialize_some_other_stuff
方法是类的私有方法,因此您不应该关心它是否执行。也就是说,如果该方法执行一些您不希望测试等待的昂贵操作,那么模拟该操作是完全可以的。因此,如果 Foo 看起来像这样:
那么您可以像这样模拟对
Bar#long_running_operation
的调用:现在,您正在测试分配。但是,您不会等待昂贵的操作完成。
Since the
initialize_some_other_stuff
method is private to the class, you should not care if it executes or not. That said, if that method performs some expensive operation that you don't want your test waiting for, then it is quite okay to mock that operation.So, if Foo looked like this:
Then you could mock out the call to
Bar#long_running_operation
like this:Now, you're testing the assignments. But, you're not waiting on the expensive operation to complete.