繁体   English   中英

HTTP Azure 函数单元测试

[英]HTTP Azure Function UnitTest

我有 http Azure 函数,我正在编写该函数的单元测试。 我得到以下错误

消息:测试方法 Functions.Tests.CacheFunctionTests.CacheRefreshFunction 抛出异常:System.InvalidOperationException:请求没有关联的配置对象或提供的配置为空。

单元测试

[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:请求没有关联的配置对象或提供的配置为空。

正如 Jocke 所说,在 httpserver 端测试请求时,您需要为请求提供 HttpConfiguration。

为了解决这个问题,我们需要通过 Properties 字典添加一个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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM