我正在使用 minitest 测试 ruby 应用程序
我有这样的场景:
class TestExample
def test_method
SomeOtherClass.new.print_message "Hello World!!!!!"
end
end
这里我想模拟SomeOtherClass
的print_message
方法,并像这样尝试
mock = MiniTest::Mock.new
test_example = TestExample.new
mock.expect(SomeOtherClass.new, nil, ["Hello World!!!!!"])
test_example.test_method
mock.verify
它不起作用,它给出了如下异常:-
MockExpectationError:预期 #("Hello World!!!!!") => [],得到 []
感谢您的建议和回答。
最佳答案
class TestMocking < MiniTest::Unit::TestCase
def test_mocking
some_other_class_mock = MiniTest::Mock.new
some_other_class_mock.expect :print_message, nil, ["Hello World!!!!!"]
SomeOtherClass.stub :new, some_other_class_mock do
test_example = TestExample.new
test_example.test_method
end
some_other_class_mock.verify
end
end
关于ruby - 如何在minitest中模拟另一个类的实例方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20018094/