英文:
Why is the ConfigureWebHost(IWebHostBuilder builder) of the WebApplicationFactory class not being called?
问题
我正在编写一个集成测试,遵循它的MSdoc。
我正在使用自定义的 WebApplicationFactory<Program>
,在其中我需要覆盖 ConfigureWebHost(IWebHostBuilder builder)
方法来注册一些服务并使用我选择的数据库,与生产环境中的不同。
问题是这个方法没有被调用。我正在做与文档中相同的事情,只是我没有使用 XUnit。我使用的是 MSTest,它没有神奇的 IClassFixture
类型。
我尝试过 ConfigureTestServices
和 ConfigureServices
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
base.ConfigureWebHost(builder);
builder.UseEnvironment(Environments.Development)
.ConfigureTestServices(services =>
{
var dbContextDescriptor = services.SingleOrDefault(
d => d.ServiceType ==
typeof(DbContextOptions<MyDbContext>));
为什么这个方法没有被调用,如何让它被调用?
编辑 添加我的设置
测试本身,继承了 CustomWebApplicationFactory
类
[TestClass]
public class TransactionTest : CustomWebApplicationFactory<Program>
{
private readonly CustomWebApplicationFactory<Program> _factory;
public TransactionTest(
CustomWebApplicationFactory<Program> factory)
{
this._factory = factory;
}
[TestMethod]
public async Task Only_existing_users_can_transact()
{
var requestParam = new MyRequest
{
phone = "12345678",
};
... 用于操作和断言的一些其他代码
}
}
设置
public class CustomWebApplicationFactory<TProgram> : WebApplicationFactory<TProgram> where TProgram : class
{
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
base.ConfigureWebHost(builder);
builder.ConfigureServices(services =>
{
//在这里添加断点没有任何效果。断点永远不会被触发
var dbContextDescriptor = services.SingleOrDefault(
d => d.ServiceType ==
typeof(DbContextOptions<AppDbContext>));
services.Remove(dbContextDescriptor);
var dbConnectionDescriptor = services.SingleOrDefault(
d => d.ServiceType ==
typeof(DbConnection));
services.Remove(dbConnectionDescriptor);
});
builder.UseEnvironment("Development");
}
}
英文:
I am writing an integration test, following its MSdoc.
I am using a Custom WebApplicationFactory<Program>
, where I need to override the ConfigureWebHost(IWebHostBuilder builder)
method to register some services and use a database of my choice, different from the one in prod.
The problem is this method is not being called. I'm doing exactly what that doc did, only that I'm not using XUnit. I use MSTest, which does not have the magical IClassFixture
type.
I have tried the ConfigureTestServices
and ConfigureServices
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
base.ConfigureWebHost(builder);
builder.UseEnvironment(Environments.Development)
.ConfigureTestServices(services =>
{
var dbContextDescriptor = services.SingleOrDefault(
d => d.ServiceType ==
typeof(DbContextOptions<MyDbContext>));
Why is the method not being called and how do I get it called?
Edit Adding my setup
The test itself, inheriting the CustomWebApplicationFactory
class
[TestClass]
public class TransactionTest : CustomWebApplicationFactory<Program>
{
private readonly CustomWebApplicationFactory<Program> _factory;
public TransactionTest(
CustomWebApplicationFactory<Program> factory)
{
this._factory = factory;
}
[TestMethod]
public async Task Only_existing_users_can_transact()
{
var requestParam = new MyRequest
{
phone = "12345678",
};
... some more code for acting and assertion
}
}
The setup
public class CustomWebApplicationFactory<TProgram> : WebApplicationFactory<TProgram> where TProgram : class
{
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
base.ConfigureWebHost(builder);
builder.ConfigureServices(services =>
{
//Adding a breakpoint here does nothing. The breakpoint is never hit
var dbContextDescriptor = services.SingleOrDefault(
d => d.ServiceType ==
typeof(DbContextOptions<AppDbContext>));
services.Remove(dbContextDescriptor);
var dbConnectionDescriptor = services.SingleOrDefault(
d => d.ServiceType ==
typeof(DbConnection));
services.Remove(dbConnectionDescriptor);
});
builder.UseEnvironment("Development");
}
}
答案1
得分: 2
When you use MSTest or NUnit, you have to initialize WebApplicationFactory
.
Side note: Usually, when I need to change registered services, I don't customize WebApplicationFactory
(as you) but customize the client with WithWebHostBuilder()
.
英文:
When you use MSTest or NUnit you have to initialize WebApplicationFactory
.
[TestClass]
public sealed class ExampleTests : IDisposable
{
private CustomWebApplicationFactory<Program> webApplicationFactory;
public void Dispose()
{
this.webApplicationFactory.Dispose();
}
[TestCleanup]
public void TestCleanup()
{
this.webApplicationFactory.Dispose();
}
[TestMethod]
public async Task TestMethodToTestSomething()
{
// Arrange
this.webApplicationFactory = new CustomWebApplicationFactory<Program>();
var httpClient = this.webApplicationFactory.CreateClient();
// Act
// ...
// Assert
// ...
}
}
Side note:
Usually, when I need to change registered services, I don't customize WebApplicationFactory
(as you) but customize the client with WithWebHostBuilder()
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论