小编Kir*_*ule的帖子

无法在 NUnit Mock 的 SetupSequence 中使用多次返回

我正在编写单元测试用例,我必须根据所需的参数返回多个响应。当我尝试下面的代码时,它运行良好。

_mockClient.SetupSequence(c => c.HttpGet(It.IsAny<string>(), It.IsAny<Dictionary<string, string>>()))
     .Returns(new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(_bucketResponseJson) })
     .Returns(new HttpResponseMessage());
Run Code Online (Sandbox Code Playgroud)

但是当我的响应依赖于传递的参数时,我将以下代码与 lambda 表达式一起使用。

 _mockClient.SetupSequence(c => c.HttpGet(It.IsAny<string>(), It.IsAny<Dictionary<string, string>>()))
                 .Returns(((string url, Dictionary<string, string> headers) =>
                 {
                     return _objectStoreCache[headers[HeaderValue]] as HttpResponseMessage;
                 })).Returns(new HttpResponseMessage());
Run Code Online (Sandbox Code Playgroud)

这给了我编译错误:

“无法将 lambda 表达式转换为类型 'System.Net.Http.HttpResponseMessage',因为它不是委托类型”

c# lambda unit-testing moq nunit-3.0

2
推荐指数
1
解决办法
718
查看次数

标签 统计

c# ×1

lambda ×1

moq ×1

nunit-3.0 ×1

unit-testing ×1