首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >有与Spring4D对应的WillReturnDefault函数吗

有与Spring4D对应的WillReturnDefault函数吗
EN

Stack Overflow用户
提问于 2019-06-03 11:11:55
回答 1查看 186关注 0票数 3

Delphi有一个WillReturnDefault方法,当您不关心函数的参数时。我不知道如何用Spring4D模拟来实现这个目的。感谢你的帮助!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-06-05 21:28:21

您可以在其默认动态模式中使用模拟,其中它允许任何调用,并且只从其方法返回默认值,或者使用param匹配器-请参见下面的示例:

代码语言:javascript
复制
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.
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56426153

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档