如何使用minitest / mock进行存根调用

时间:2015-06-05 09:45:48

标签: ruby-on-rails mocking minitest

参见示例。它不是真正的代码,但显示问题。 在代码生成器中使用其他args调用两次。我想检查一个调用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

1 个答案:

答案 0 :(得分:1)

Stubbed值正在块中工作,所以你可能应该:

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