我正在编写单元测试用例,我必须根据所需的参数返回多个响应。当我尝试下面的代码时,它工作得很好。
_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());
但是当我的响应依赖于传递的参数时,我将使用以下带有 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());
这给了我编译错误:
"Cannot convert lambda expression to type 'System.Net.Http.HttpResponseMessage' because it is not a delegate type"
最佳答案
您在设置中尝试执行的操作无法按 ISetupSequentialResult<TResult>
完成不允许将 lambda 表达式而是具体值传递给 Returns
方法。也没有允许该功能的扩展方法。
public interface ISetupSequentialResult<TResult> {
//...
// Summary:
// Returns value
ISetupSequentialResult<TResult> Returns(TResult value);
//...
}
关于c# - 无法通过 NUnit Mock 的 SetupSequence 使用多个返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40242809/