gpt4 book ai didi

c# - .NET6隔离Azure功能单元和集成测试导致gRPC异常

转载 作者:行者123 更新时间:2023-12-02 07:18:39 26 4
gpt4 key购买 nike

我有一个独立的 Azure 函数,可以进行几个 HTTP POST 调用。我正在尝试为他们编写集成测试。但测试设置失败并出现 gRPC 错误。

这里是使用 services.AddHttpClient() 配置 HttpClient 的 Program.cs;

public class Program
{

public static void Main()
{
var host = new HostBuilder()
.ConfigureFunctionsWorkerDefaults()
.ConfigureServices(services =>
{
services.AddHttpClient();
}).Build();

host.Run();
}
}

示例函数如下所示:

public class AzFunctionEndpoint
{
public AzFunctionEndpoint(IHttpClientFactory httpClientFactory, IConfiguration configuration, ILoggerFactory loggerFactory)
{
logger = loggerFactory.CreateLogger<ResolveEndpoint>();
this.httpClientFactory = httpClientFactory;
this.configuration = configuration;
}

[Function("azfunction")]
public async Task<HttpResponseData> Run([HttpTrigger(AuthorizationLevel.Function, "post", Route = "azs/azfunction")] HttpRequestData req)
{

// Couple of other HTTP calls using httpClientFactory

// return
var res = req.CreateResponse(HttpStatusCode.OK);
return res;

}

private readonly IConfiguration configuration;
private readonly IHttpClientFactory httpClientFactory;
private readonly ILogger logger;
}

该函数在本地计算机上以及部署到 Azure 时正确运行。

现在我尝试创建一个集成测试

public class AzFunctionEndpointIntegrationTests
{
public AzFunctionEndpointIntegrationTests()
{
factory = new WebApplicationFactory<Program>();
var clientFactory = factory.Services.GetService<IHttpClientFactory>();

// THIS LINE CAUSES gRPC host error
client = clientFactory.CreateClient();
}

[Fact]
public async Task AzFunction_Should_BeOK()
{
// POST to the azure function
HttpRequestMessage request = new HttpRequestMessage(new HttpMethod(HttpMethods.Post), "api/azs/azfunction");

var response = await client.SendAsync(request);

response.StatusCode.Should().Be(System.Net.HttpStatusCode.OK);
}

private HttpClient client;
private WebApplicationFactory<Program> factory;
}

尝试创建 HttpClient 来调用我的函数的测试代码导致此异常

client = clientFactory.CreateClient();

System.InvalidOperationException:无法解析 gRPC channel URI“http://:51828”。

我不太明白这个错误是什么!感谢您为 Azure Functions 编写集成测试提供任何帮助。

最佳答案

System.InvalidOperationException : The gRPC channel URI'http://:51828' could not be parsed.

I don't quite understand what is this error ! Any help on writingintegration tests for Azure Functions is appreciated.

以下是一些可能有助于解决上述问题的解决方法。

  • 基于此GitHub issue我们观察到的根本原因是,可能是由于您使用的 IDE 使用了不正确的运行/调试配置。

请确保您使用与给定 gitHub 链接 上建议的相同的方式来运行应用程序。

写一个Integration test you can refer this Example for your function app .

示例代码示例:-

test.cs

[TestClass]
public class DefaultHttpTriggerTests
{
private HttpClient _http;

[TestInitialize]
public void Initialize()
{
this._http = new HttpClient();
}

[TestCleanup]
public void Cleanup()
{
this._http.Dispose();
}

[TestMethod]
public async Task Given_OpenApiUrl_When_Endpoint_Invoked_Then_It_Should_Return_Title()
{
// Arrange
var requestUri = "http://localhost:7071/api/openapi/v3.json";

// Act
var response = await this._http.GetStringAsync(requestUri).ConfigureAwait(false);
var doc = JsonConvert.DeserializeObject<OpenApiDocument>(response);

// Assert
doc.Should().NotBeNull();
doc.Info.Title.Should().Be("OpenAPI Document on Azure Functions");
doc.Components.Schemas.Should().ContainKey("greeting");

var schema = doc.Components.Schemas["greeting"];
schema.Type.Should().Be("object");
schema.Properties.Should().ContainKey("message");

var property = schema.Properties["message"];
property.Type.Should().Be("string");
}
}

有关更多信息,请参阅以下链接:-

所以线程:- System.InvaliOperationException: The gRPC channel URI 'http://0' could not be parsed .

微软文档:- Guide for running C# Azure Functions in an isolated process

关于c# - .NET6隔离Azure功能单元和集成测试导致gRPC异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71273358/

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