【问题标题】:FakeItEasy Returning Func with origin arguments in C#FakeItEasy 在 C# 中返回具有原始参数的 Func
【发布时间】:2020-09-16 07:00:34
【问题描述】:

我尝试使用 FakeItEasy 运行一些测试,我的目的是将一种方法替换为另一种方法以获得不同的返回值。对我来说棘手的事情是将源调用参数重定向到替换方法。调用工作正常,但不改变结果。 (我可以做错什么)

这是我想出来的

Func<string, string> func = delegate (string request)
{
   return $"Replaced {request}";
};
var service = A.Fake<MyService>();

A.CallTo(() => service.DoAction(A<string>._)).Returns<string>(func(A<string>._));
// func(A<string>._) cause an exception, i need to have arg from DoAction here

// following line works fine but dont change return
A.CallTo(() => service.DoAction(A<string>._)).Invokes((string request) => func(request));

// result is empty here but should have "Replaced Test"
var result = service.DoAction("Test");

有人能指出正确的方向吗?

【问题讨论】:

    标签: c# fakeiteasy


    【解决方案1】:

    我想你正在寻找

    A.CallTo(() => service.DoAction(A<string>._)) 
        .ReturnsLazily((string request) => func(request));
    

    您可以在文档的 Return Values Calculated at Call Time 主题中了解更多信息。

    【讨论】:

      猜你喜欢
      • 2017-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-11
      • 1970-01-01
      • 2018-11-28
      • 1970-01-01
      相关资源
      最近更新 更多