开发者

Test method call in the constructor

开发者 https://www.devze.com 2023-04-04 02:51 出处:网络
I have the following code 开发者_开发知识库class SomeClass def initialize(opts) if opts[:should_load]

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

精彩评论

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