在我的测试中,我想为类的任何实例存根一个预设的响应。
它可能看起来像这样:
Book.stubs(:title).any_instance().returns("War and Peace")
然后每当我打电话时@book.title
它返回“战争与和平”。
有没有办法在 MiniTest 中做到这一点?
如果是,您能给我一个示例代码片段吗?
或者我需要摩卡之类的东西吗?
MiniTest 确实支持 Mocks,但 Mocks 对于我的需要来说太过分了。
# Create a mock object
book = MiniTest::Mock.new
# Set the mock to expect :title, return "War and Piece"
# (note that unless we call book.verify, minitest will
# not check that :title was called)
book.expect :title, "War and Piece"
# Stub Book.new to return the mock object
# (only within the scope of the block)
Book.stub :new, book do
wp = Book.new # returns the mock object
wp.title # => "War and Piece"
end
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)