开发者

Test method call in the constructor

I have the following code

开发者_开发知识库class SomeClass

  def initialize(opts)
    if opts[:should_load]
      load
    else
      setup(opts[:path])
    end
  end

  def load; end

  def setup; end
end

And I want to test that the appropriate method is being called, but I cannot figure out how to do this with RSpec. Any tips?


Play with Object#any_instance (rspec >= 2.6.0):

SomeClass.any_instance.should_receive(:load)
SomeClass.new(:should_load => true)

SomeClass.any_instance.should_receive(:setup).with("mypath")
SomeClass.new(:path => "mypath")
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜