HTTP Azure功能单元测试

时间:2019-05-03 11:38:23

标签: c# unit-testing azure-functions

我具有http Azure功能,并且正在编写该功能的单元测试。我遇到错误

  

消息:测试方法CAT.AMS.Functions.Tests.RuleCacheRefreshFunctionTests.RuleCacheRefreshFunction引发异常:   System.InvalidOperationException:该请求没有关联的配置对象,或者提供的配置为空。

UnitTest

[TestMethod]
    public async Task RuleCacheRefreshFunction()
    {
        // Arrange
        mockLog.Setup(x => x.Info(It.IsAny<string>()));
        mockRuleService.Setup(x => x.RefreshRuleCache()).Returns(Task.FromResult(0));
        HttpRequestMessage httpRequestMessage = new HttpRequestMessage() {
            RequestUri = new System.Uri("http://localhost:0895/RuleCacheRefreshFunction"),
            Method = HttpMethod.Get                
        };            
        // Act
        await CAT.AMS.RuleFunctions.RuleCacheRefreshFunction.Run(httpRequestMessage, mockRuleService.Object, mockLog.Object);

        // Assert
        Assert.IsTrue(true);
    }

我想,我没有在“ HttpRequestMessage”属性中传递一些基本值。有想法吗?

1 个答案:

答案 0 :(得分:0)

  

System.InvalidOperationException:该请求没有关联的配置对象,或者提供的配置为空。

正如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