Kir*_*ule 2 c# lambda unit-testing moq nunit-3.0
我正在编写单元测试用例,我必须根据所需的参数返回多个响应。当我尝试下面的代码时,它运行良好。
_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',因为它不是委托类型”
您在设置中尝试执行的操作无法完成,因为ISetupSequentialResult<TResult>不允许将 lambda 表达式而是将具体值传递给Returns方法。也没有允许该功能的扩展方法。
public interface ISetupSequentialResult<TResult> {
//...
// Summary:
// Returns value
ISetupSequentialResult<TResult> Returns(TResult value);
//...
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
718 次 |
| 最近记录: |