gpt4 book ai didi

ruby - 我如何在 MiniTest 中 stub ?

转载 作者:数据小太阳 更新时间:2023-10-29 06:20:17 25 4
gpt4 key购买 nike

在我的测试中,我想为一个类的任何实例 stub 一个 jar 装响应。

它可能看起来像这样:

Book.stubs(:title).any_instance().returns("War and Peace")

然后每当我调用 @book.title 时,它都会返回“ war 与和平”。

有没有办法在 MiniTest 中做到这一点?如果是,你能给我一个示例代码片段吗?

或者我需要像 mocha 这样的东西吗?

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

关于ruby - 我如何在 MiniTest 中 stub ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7211086/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com