Ruby 构造函数中的测试方法调用
我有以下代码Ruby 构造函数中的测试方法调用,ruby,rspec,Ruby,Rspec,我有以下代码 class SomeClass def initialize(opts) if opts[:should_load] load else setup(opts[:path]) end end def load; end def setup; end end 我想测试是否调用了适当的方法,但我不知道如何使用RSpec实现这一点。任何提示?使用对象#任何实例(rspec>=2.6.0): 我正在使用RSpec2.6
class SomeClass
def initialize(opts)
if opts[:should_load]
load
else
setup(opts[:path])
end
end
def load; end
def setup; end
end
我想测试是否调用了适当的方法,但我不知道如何使用RSpec实现这一点。任何提示?使用对象#任何实例
(rspec>=2.6.0):
我正在使用RSpec2.6.0,但是我得到了“为RSpec调用的私有方法pretty_print”。谷歌没有关于这个错误的信息,你以前遇到过吗?@providence:对不起,答案是编辑过的,很明显对象实例化会返回对象本身。现在我只检查电话是否打了。非常感谢,这非常有帮助。普罗维登斯,我收到了与此完全无关的打印错误,你找到解决方法了吗?
SomeClass.any_instance.should_receive(:load)
SomeClass.new(:should_load => true)
SomeClass.any_instance.should_receive(:setup).with("mypath")
SomeClass.new(:path => "mypath")