我无法使用这个简单的 mocha 测试
Diclaimer 我是摩卡的新手!
我的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)
答案 0 :(得分: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