testing - 在集成测试中访问内存 dbcontext
问题描述
如何在集成测试中访问内存数据库的 dbcontext?
并进行类似的测试:
public class IndexPageTests :
IClassFixture<CustomWebApplicationFactory<RazorPagesProject.Startup>>
{
private readonly HttpClient _client;
private readonly CustomWebApplicationFactory<RazorPagesProject.Startup>
_factory;
public IndexPageTests(
CustomWebApplicationFactory<RazorPagesProject.Startup> factory)
{
_factory = factory;
_client = factory.CreateClient(new WebApplicationFactoryClientOptions
{
AllowAutoRedirect = false
});
}
在这个 IndexPageTests 中是否可以访问内存中的 dbcontext?
我努力了
using (var context = new ApplicationDbContext(???))
我需要访问之前从 CustomWebApplicationFactory 播种的表中的数据
但不确定要为选项添加什么
解决方案
感谢 Nikosi,这是我设法获得 dbcontext 的方式
var scopeFactory = _factory.Server.Host.Services.GetService<IServiceScopeFactory>();
using (var scope = scopeFactory.CreateScope())
{
var context = scope.ServiceProvider.GetService<ApplicationDbContext>();
}
推荐阅读
- php - 在 PHP 中将 CSV 转换为对象数组
- amazon-web-services - 并非所有打开 aws 页面的网址
- python - 使用转换器参数传递“字符串”时,Pandas read_excel 新字符串类型错误
- python - Gspread:一次格式化特定单元格(不仅仅是一行或一列)
- javascript - 打字稿:字符串和字符串模板关系 - 转换?
- r - R调查后分层:与调查功能作斗争
- angular - 如何使用 FormArray 将数据加载到表单中,将其显示在表格中,然后一次性发送加载的数据?
- python - PyQt6 的 Qt 模块替代品
- python - 填充点之间的颜色渐变
- sql - 验证动态 SQL DELETE 语句而不执行该语句