Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Delphi 是否有一个Spring4D对应函数返回默认值_Delphi_Spring4d_Delphi Mocks - Fatal编程技术网

Delphi 是否有一个Spring4D对应函数返回默认值

Delphi 是否有一个Spring4D对应函数返回默认值,delphi,spring4d,delphi-mocks,Delphi,Spring4d,Delphi Mocks,Delphi Mocks有一个WillReturnDefault方法,当您不关心函数的参数时。我不知道如何使用Spring4D mocking实现这一点。谢谢你的帮助 您可以在默认的动态模式下使用mock,在该模式下,mock允许任何调用并仅从其方法返回默认值,或者使用param matchers-请参见以下示例: uses Spring.Mocking; type {$M+} ITest = interface function GiveNumber(const s: s

Delphi Mocks有一个
WillReturnDefault
方法,当您不关心函数的参数时。我不知道如何使用Spring4D mocking实现这一点。谢谢你的帮助

您可以在默认的动态模式下使用mock,在该模式下,mock允许任何调用并仅从其方法返回默认值,或者使用param matchers-请参见以下示例:

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.
使用
春天。嘲笑;
类型
{$M+}
ITest=接口
函数GiveNumber(const s:string):整数;
结束;
变量
m:模拟;
开始
//mock在默认情况下是动态的,因此它们允许所有调用发生并返回默认值
Writeln(m.Instance.GiveNumber(“”));
//参数匹配器可以应用于When调用-
//这里我们使用内置的Args.Any来允许任何参数发生
//传递给GiveNumber的实际值并不重要
m、 Setup.Returns(42).When(Args.Any).GiveNumber(“”);
Writeln(m.Instance.GiveNumber('whatever');
//当指定一个特定的参数匹配器时,基本上是将其添加到现有行为中
//调用模拟检查时,从
//最近定义的
m、 Setup.Returns(77).When.GiveNumber(Arg.IsEqual('this'));
Writeln(m.Instance.GiveNumber('this'));//正如我们刚才所说的
Writeln(m.Instance.GiveNumber('something');//42正如我们之前为任何参数指定的
//因此,您应该始终从更广泛的匹配器开始,然后是更具体的匹配器
//正如你现在所看到的,一个更广泛的概念会“覆盖”一个更具体的概念
m、 Setup.Returns(42).When(Args.Any).GiveNumber(“”);
//我们现在得到42个参数。任何匹配器都是最后添加的,并且匹配参数
Writeln(m.Instance.GiveNumber('this'));
Readln;
结束。