参见示例。它不是真正的代码,但显示了问题。 在代码生成器中使用其他参数调用两次。我想检查一个带有 arg install 的调用是否被调用,并 stub 其他调用
def test
bundle_command_mock = Minitest::Mock.new
bundle_command_mock.expect(:call, nil, ['install'])
generator.stub(:bundle_command, bundle_command_mock) do |g|
g.bundle_command('install')
g.bundle_command('exec spring binstub --all') # <-- This call raise error No more expects available for :call: ["exec spring binstub --all"]
end
bundle_command_mock.verify
end
有可能吗?我知道这在摩卡咖啡中是可能的。请参阅 mocha 中的工作示例
def test
generator.expects(:bundle_command).with('install').once
generator.stubs(:bundle_command).with('exec spring binstub --all')
generator.bundle_command("install")
generator.bundle_command("exec spring binstub --all")
end
最佳答案
stub 值仅在 block 内工作,因此您可能应该:
def test
bundle_command_mock = Minitest::Mock.new
bundle_command_mock.expect(:call, nil, ['install'])
generator.stub(:bundle_command, bundle_command_mock) do |g|
g.bundle_command('install')
g.bundle_command('exec spring binstub --all') # <-- This call raise error No more expects available for :call: ["exec spring binstub --all"]
end
bundle_command_mock.verify
end
关于ruby-on-rails - 如何使用 minitest/mock 进行带有 Expect 的 stub 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30663369/