gpt4 book ai didi

C#,如何模拟 Azure 订阅 - 列出位置

转载 作者:行者123 更新时间:2023-12-03 03:33:26 25 4
gpt4 key购买 nike

我需要用模拟来实现方法来获取物理区域 Azure Subscriptions - List Locations ,不确定它应该是什么样子

public async Task<IEnumerable<Region>> GetPhysicalRegions(CancellationToken token = default(CancellationToken)){}

最佳答案

您使用本页定义的 IHttpClientFactory 实现 HttpClient,选择最适合您的解决方案: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/http-requests?view=aspnetcore-6.0

然后,如果您实现一个接口(interface),例如 IMyAzureClient,那么您可以为注入(inject)此接口(interface)的任何位置创建一个 stub 或模拟。

public sealed class MyAzureClientTests
{
private Mock<HttpMessageHandler> _httpMessageHandlerMock;

private MyAzureClient _sut;

[SetUp]
public void SetUp()
{
_httpMessageHandlerMock = new Mock<HttpMessageHandler>();

var httpClient = new HttpClient(_httpMessageHandlerMock.Object);
httpClient.BaseAddress = new Uri("https://unit-tests:1337/");
_sut = new MyAzureclient(
httpClient,
);
}

[Test]
public async Task YourTest()
{
// arrange
var inputModel = new Fixture().Create<SomeInputModel>();
var expectedJson = "{}";

_httpMessageHandlerMock.Protected()
.Setup<Task<HttpResponseMessage>>("SendAsync", ItExpr.IsAny<HttpRequestMessage>(), ItExpr.IsAny<CancellationToken>())
.ReturnsAsync(new HttpResponseMessage {
StatusCode = HttpStatusCode.OK,
Content = new StringContent(expectedJson, Encoding.UTF8)
}).Verifiable();

// act
var actual = await _sut.SomeMethodAsync(inputModel);

// assert
_httpMessageHandlerMock.Protected().Verify(
"SendAsync",
Times.Once(),
ItExpr.IsAny<HttpRequestMessage>(),
ItExpr.IsAny<CancellationToken>()
);
Assert.AreEqual(Guid.Parse("efc6f86b-f40c-4dce-baef-3bfae0bcdb08"), actual.MyProperty);
}

如果您在开发环境中运行此程序,请考虑使用 stub ,并根据它接收到的参数模拟行为。

关于C#,如何模拟 Azure 订阅 - 列出位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74182983/

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