1

我无法让这个简单的摩卡测试工作

免责声明我是摩卡的新手!

我的Factory班级

# lib/fabtory.rb
class Factory
  def self.build klass, *args
    klass.new *args
  end
end

我的测试代码

# test/test_factory.rb
class FactoryTest < MiniTest::Unit::TestCase

  # my fake class
  class Fake
    def initialize *args
    end
  end

  def test_build_passes_arguments_to_constructor
    obj = Factory.build Fake, 1, 2, 3
    obj.expects(:initialize).with(1, 2, 3).once
  end

end

输出

unsatisfied expectations:
- expected exactly once, not yet invoked: #<Fake:0x7f98d5534740>.initialize(1, 2, 3)
4

1 回答 1

2

expects方法在他的调用之后查找方法调用。

你必须设置一些不同的东西:

def test_build_passes_arguments_to_constructor
  fake = mock()
  Fake.expects(:new).with(1, 2, 3).returns(fake)
  assert_equal fake, Factory.build(Fake, 1, 2, 3)
end
于 2013-06-20T02:32:12.290 回答