我有一个应用程序当前按设计工作,但在扩展它之前,我尝试使用 xUnit 设置集成测试。目前测试在执行测试时只会使用原始服务,我不明白为什么。
这是测试:
using IStoreRepository = Repositories.V3.Interfaces.IStoreRepository;
public class StoreTests : IClassFixture<WebApplicationFactory<Startup>> {
private readonly ITestOutputHelper _output;
private readonly WebApplicationFactory<Startup> _factory;
private readonly string _url;
public StoreTests(ITestOutputHelper output, WebApplicationFactory<Startup> factory) {
_output = output;
_factory = factory;
_url = "/api/store";
}
[Theory]
[InlineData("GET", "FAKE123")]
public async Task StoreByCode(string method, string code = null) {
// var client = _factory.CreateClient();
var client = _factory.WithWebHostBuilder(builder => {
builder.ConfigureTestServices(services => {
services.AddScoped<IStoreRepository, StoreRepositoryTest>();
});
}).CreateClient();
var request = new HttpRequestMessage(new HttpMethod(method), $"{_url}/{code}");
string readAsStringAsync;
_output.WriteLine($"Request Uri: {request.RequestUri}");
using (var response = await client.SendAsync(request)) {
response.EnsureSuccessStatusCode();
readAsStringAsync = await response.Content.ReadAsStringAsync();
if (!response.IsSuccessStatusCode) {
_output.WriteLine($"Not successful ({response.StatusCode}): {readAsStringAsync}");
}
}
var stores = JsonConvert.DeserializeObject<List<Store>>(readAsStringAsync);
Assert.True(stores.Any());
}
}
Repository然而,当我进行测试时,实际中的断点StoreRepository是在 Startup.cs 中注册的,而不是 中的断点StoreRepositoryTest。我设置我的工厂来覆盖依赖项,但它忽略了它。我可以做什么来纠正这个问题。
作为参考,我一直在使用此来源:https://learn.microsoft.com/en-us/aspnet/core/test/integration-tests ?view=aspnetcore-2.2
HUWWW
慕莱坞森
相关分类