gpt4 book ai didi

c# - Azure Functions 单元测试错误 (TimerTrigger.HttpFunction)

转载 作者:太空宇宙 更新时间:2023-11-03 11:57:35 25 4
gpt4 key购买 nike

我目前正在按照本指南 ( https://learn.microsoft.com/en-us/azure/azure-functions/functions-test-a-function ) 向我的 Azure Functions 应用程序添加测试。

目前我已经构建了 8 个运行良好的 Azure Functions,我还添加了一个 Functions.Tests 项目并在其中引用了 Azure Functions 项目。

enter image description here

这是 Functions.Tests 目前的样子。

using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Xunit;

namespace Functions.Tests
{
public class FunctionsTests
{
private readonly ILogger logger = TestFactory.CreateLogger();

[Fact]
public async void Http_trigger_should_return_known_string()
{
var request = TestFactory.CreateHttpRequest("name", "Bill");
var response = (OkObjectResult)await HttpFunction.Run(request, logger);
Assert.Equal("Hello, Bill", response.Value);
}

[Theory]
[MemberData(nameof(TestFactory.Data), MemberType = typeof(TestFactory))]
public async void Http_trigger_should_return_known_string_from_member_data(string queryStringKey, string queryStringValue)
{
var request = TestFactory.CreateHttpRequest(queryStringKey, queryStringValue);
var response = (OkObjectResult)await HttpFunction.Run(request, logger);
Assert.Equal($"Hello, {queryStringValue}", response.Value);
}

[Fact]
public void Timer_should_log_message()
{
var logger = (ListLogger)TestFactory.CreateLogger(LoggerTypes.List);
TimerTrigger.Run(null, logger);
var msg = logger.Logs[0];
Assert.Contains("C# Timer trigger function executed at", msg);
}
}
}

但是我在 FunctionsTests.cs 中收到以下错误

enter image description here

我已尝试了 Visual Studio 中建议的所有修复,并在线检查了资源,但没有成功。也许我缺少引用?我不确定,因为我已经逐字逐句地遵循了指南。

使用的 Azure 函数示例:

using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;

namespace Exemplar
{
public static class getCase
{
[FunctionName("getCase")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "get", Route = "v1/case/caseId")] HttpRequest req,
ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");

string name = req.Query["name"];

string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
dynamic data = JsonConvert.DeserializeObject(requestBody);
name = name ?? data?.name;

return name != null
? (ActionResult)new OkObjectResult($"Hello, {name}")
: new BadRequestObjectResult("Please pass a name on the query string or in the request body");
}
}

}

最佳答案

假设引用了提供的包含该函数的项目,测试只需按照链接示例中所示的方式安排和执行目标函数

[Fact]
public async Task getCase_should_return_known_string()
{
var request = TestFactory.CreateHttpRequest("name", "Bill");
var response = (OkObjectResult)await getCase.Run(request, logger);
Assert.Equal("Hello, Bill", response.Value);
}

[Theory]
[MemberData(nameof(TestFactory.Data), MemberType = typeof(TestFactory))]
public async Task getCase_should_return_known_string_from_member_data(string queryStringKey, string queryStringValue)
{
var request = TestFactory.CreateHttpRequest(queryStringKey, queryStringValue);
var response = (OkObjectResult)await getCase.Run(request, logger);
Assert.Equal($"Hello, {queryStringValue}", response.Value);
}

同时避免使用async void。重构这些测试以使用async Task代替。

关于c# - Azure Functions 单元测试错误 (TimerTrigger.HttpFunction),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58879231/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com