C# 如何在多个xunit测试中保持DB连接?
我得到了执行基本CRUD活动的服务类,我想一起运行所有测试 目前,我可以一个接一个地运行测试。但是,如果我通过选择测试类来运行测试,它只会首先运行随机测试,然后失败其他显示错误的测试:C# 如何在多个xunit测试中保持DB连接?,c#,asp.net-core,xunit,xunit.net,C#,Asp.net Core,Xunit,Xunit.net,我得到了执行基本CRUD活动的服务类,我想一起运行所有测试 目前,我可以一个接一个地运行测试。但是,如果我通过选择测试类来运行测试,它只会首先运行随机测试,然后失败其他显示错误的测试: The connection string property has not been initialized. 原因是我在我的服务中使用了using子句,并且在第一次测试之后它就摆脱了当前连接 CashupService.cs public class CashupService {
The connection string property has not been initialized.
原因是我在我的服务中使用了using
子句,并且在第一次测试之后它就摆脱了当前连接
CashupService.cs
public class CashupService
{
private PJDbContext _context;
public CashupService(PJDbContext context)
{
_context = context;
}
public async Task<int> InsertCashUpRecord(CashupModel model)
{
using (IDbConnection conn = _context.Database.GetDbConnection())
{
var parameters = new
{
JobID = model.JobID
, Description = model.Description
, Amount = model.Amount
, User = model.UserCreated
};
return await conn.ExecuteAsync("dbo.InsertCashUpRecord", parameters, commandType: CommandType.StoredProcedure);
}
}
public async Task<IEnumerable<CashupModel>> GetCashUpRecords(bool Latest, DateTime? From, DateTime? To)
{
using (IDbConnection conn = _context.Database.GetDbConnection())
{
var parameters = new
{
Latest = Latest
, From = From
, To = To
};
return await conn.QueryAsync<CashupModel>("dbo.GetCashUpRecords", parameters, commandType: CommandType.StoredProcedure);
}
}
public async Task<int> InsertCashUpLog(CashupLogModel model)
{
_context.CashupLogs.Add(model);
return await _context.SaveChangesAsync();
}
}
CashupServiceTests.cs
public class CashupService
{
private PJDbContext _context;
public CashupService(PJDbContext context)
{
_context = context;
}
public async Task<int> InsertCashUpRecord(CashupModel model)
{
using (IDbConnection conn = _context.Database.GetDbConnection())
{
var parameters = new
{
JobID = model.JobID
, Description = model.Description
, Amount = model.Amount
, User = model.UserCreated
};
return await conn.ExecuteAsync("dbo.InsertCashUpRecord", parameters, commandType: CommandType.StoredProcedure);
}
}
public async Task<IEnumerable<CashupModel>> GetCashUpRecords(bool Latest, DateTime? From, DateTime? To)
{
using (IDbConnection conn = _context.Database.GetDbConnection())
{
var parameters = new
{
Latest = Latest
, From = From
, To = To
};
return await conn.QueryAsync<CashupModel>("dbo.GetCashUpRecords", parameters, commandType: CommandType.StoredProcedure);
}
}
public async Task<int> InsertCashUpLog(CashupLogModel model)
{
_context.CashupLogs.Add(model);
return await _context.SaveChangesAsync();
}
}
在这里,我在构造函数中创建了DbContext和服务,理论上,当它运行另一个测试时,不应该再次创建它吗
public class CashupServiceTest : IClassFixture<PJFixture>
{
PJFixture _fixture;
CashupService _service;
public CashupServiceTest(PJFixture fixture)
{
_fixture = fixture;
_service = new CashupService(_fixture.context);
}
[Fact]
public async Task Should_Return_CashUpRecords_With_Latest()
{
var result = await _service.GetCashUpRecords(true, null, null);
Assert.NotEmpty(result);
}
[Fact]
public async Task Should_Return_CashUpRecords_With_Dates()
{
var result = await _service.GetCashUpRecords(false, new DateTime(2017, 3, 2, 10, 10, 10), DateTime.Now);
Assert.NotEmpty(result);
}
[Fact]
public async Task Should_Insert_CashupLog()
{
var model = new CashupLogModel();
model.Amount = 5;
model.UserCreated = "tester";
var result = await _service.InsertCashUpLog(model);
Assert.True(model.LogID > 0);
}
}
公共类CashupServiceTest:IClassFixture
{
PJU夹具;
现金服务(CashupService);;
公共现金服务测试(PJ7)
{
_夹具=夹具;
_服务=新的CashupService(_fixture.context);
}
[事实]
公共异步任务应返回\u CashUpRecords\u和\u Latest()
{
var result=wait_service.GetCashUpRecords(true、null、null);
Assert.NotEmpty(结果);
}
[事实]
公共异步任务应返回带有日期的现金记录
{
var result=wait_service.GetCashUpRecords(false,new DateTime(2017,3,2,10,10,10),DateTime.Now);
Assert.NotEmpty(结果);
}
[事实]
公共异步任务应插入\u CashupLog()
{
var模型=新的CashupLogModel();
型号:金额=5;
model.UserCreated=“tester”;
var result=wait_service.InsertCashUpLog(模型);
Assert.True(model.LogID>0);
}
}
你能建议我如何编写测试并一次性运行它们吗
如果我为每个测试创建另一个测试类,那个错误就会消失,但在我的测试文件中会有多个文件
我可以知道最佳实践以及你们是如何做到这一点的吗?它们是测试,它们只运行一次,只要您不使用生产数据库(并且不应该用于集成测试),您就可以安全地删除using并共享上下文。@Sxntk using在测试的类中!