delphi - Spring4D是否有WillReturnDefault对应的函数

标签 delphi spring4d delphi-mocks

当您不关心函数的参数时,

Delphi-Mocks 有一个 WillReturnDefault 方法。我不知道如何使用 Spring4D 模拟来做到这一点。感谢您的帮助!

最佳答案

您可以在其默认动态模式下使用模拟,该模式允许任何调用并仅从其方法返回默认值,或者使用参数匹配器 - 请参阅以下示例:

uses
  Spring.Mocking;

type
  {$M+}
  ITest = interface
    function GiveNumber(const s: string): Integer;
  end;

var
  m: Mock<ITest>;
begin
  // mocks are dynamic by default so they let all calls happen and return the default
  Writeln(m.Instance.GiveNumber(''));

  // parameter matcher can be either applied to the When call -
  // here we are using the built-in Args.Any to let any parameter happen
  // the actual values passed to GiveNumber does not matter then
  m.Setup.Returns(42).When(Args.Any).GiveNumber('');
  Writeln(m.Instance.GiveNumber('whatever'));

  // when specifying a specific param matcher you basically add this to the existing behavior
  // when calling the mock checks for any given behavior that matches starting from the
  // most recently defined
  m.Setup.Returns(77).When.GiveNumber(Arg.IsEqual('this'));
  Writeln(m.Instance.GiveNumber('this')); // 77 as we just specified
  Writeln(m.Instance.GiveNumber('something')); // 42 as we specified before for any arguments

  // so you should always start with the broader matcher and then the more specific ones
  // as a broader one would "override" a more specific one as you can see now
  m.Setup.Returns(42).When(Args.Any).GiveNumber('');
  // we get 42 now as the Args.Any matcher was added last and matches the parameter
  Writeln(m.Instance.GiveNumber('this'));

  Readln;
end.

关于delphi - Spring4D是否有WillReturnDefault对应的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56426153/

相关文章:

Delphi : Field 'False' not found. 仅发生在我的电脑上

php - Delphi 应用程序的 SSL 证书 - 是否需要启用安全性?

windows - 关于 JvPatchFile 组件?

delphi - Spring4d中如何从ServiceLocator获取子接口(interface)实例?

Delphi,如何避免application.CreateForm?

Delphi Mocks – 是否可以在使用 ‘VAR’ 模拟的函数中使用 ‘OUT’ 或 ‘WillReturn’ 排列?

Delphi Mocks - 验证重载方法从未被调用

windows - JumpList 到 Delphi 上所有打开的表单

delphi - 使用Delphi Mock框架并造成副作用

delphi - Spring4D TMultiMap 动态维护 IEnumerable