【问题标题】:HTTP Azure Function UnitTestHTTP Azure 函数单元测试
【发布时间】:2020-03-20 14:59:32
【问题描述】:

我有 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”属性中传递一些基本值。有什么想法吗?

【问题讨论】:

标签: c# unit-testing azure-functions


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-30
    • 1970-01-01
    • 1970-01-01
    • 2010-09-23
    相关资源
    最近更新 更多