我具有http azure函数,我正在编写该功能的单元测试。我要低于错误
消息:测试方法function.tests.cachefunctiontests.cacherefreshfunction抛出例外: system.invalidoperationException:请求没有关联的配置对象或提供的配置为null。
Unittest
[TestMethod]
public async Task CacheRefreshFunction()
{
// Arrange
mockLog.Setup(x => x.Info(It.IsAny<string>()));
mockService.Setup(x => x.RefreshCache()).Returns(Task.FromResult(0));
HttpRequestMessage httpRequestMessage = new HttpRequestMessage() {
RequestUri = new System.Uri("http://localhost:0895/CacheRefreshFunction"),
Method = HttpMethod.Get
};
// Act
await Functions.CacheRefreshFunction.Run(httpRequestMessage, mockRuleService.Object, mockLog.Object);
// Assert
Assert.IsTrue(true);
}
我想,我没有传递" httprequestmessage"属性中的一些基本价值。有什么想法吗?
system.invalidoperationException:请求没有关联的配置对象或提供的配置为null。
正如Jocke所说,在测试HTTPSERVER的请求时,您需要将请求作为HTTPConfiguration。
要解决此问题,我们需要通过属性字典添加一个 HttpConfiguration
对象:
Request = new HttpRequestMessage()
{
Properties = { { HttpPropertyKeys.HttpConfigurationKey, new HttpConfiguration() } },
RequestUri = new System.Uri("http://localhost:0895/RuleCacheRefreshFunction"),
Method = HttpMethod.Get
}
我添加了 Microsoft.AspNet.WebApi
nuget软件包以添加 System.Web.Http.HttpConfiguration