小编RJa*_*nes的帖子

Moq一个具有匿名类型的函数

我正在尝试模拟这种方法

Task<TResult> GetResultAsync<TResult>(Func<string, TResult> transformFunc)
Run Code Online (Sandbox Code Playgroud)

像这样

iMock.Setup(m => m.GetResultAsync(It.IsAny<Func<string, object>>())).ReturnsAsync(new { isPair = false });
Run Code Online (Sandbox Code Playgroud)

测试调用方法,将匿名类型传递给通用参数,例如:

instance.GetResultAsync(u => new {isPair = u == "something" }) //dont look at the function return because as generic could have diferent implementations in many case
Run Code Online (Sandbox Code Playgroud)

Moq从未将我的GetResultAsync方法与发送的参数匹配。

我正在使用Moq 4

c# unit-testing moq anonymous-types anonymous-function

5
推荐指数
1
解决办法
2316
查看次数