C# 具有多个存储库的集成测试服务
假设我有一个类C# 具有多个存储库的集成测试服务,c#,entity-framework,testing,nunit,C#,Entity Framework,Testing,Nunit,假设我有一个类EmployeeService,看起来像这样: public class EmployeeService { private readonly IRepository<Employee> _employeeRepository; public EmployeeService(IRepository<Employee> employeeRepository) { _employeeRepository = employ
EmployeeService
,看起来像这样:
public class EmployeeService
{
private readonly IRepository<Employee> _employeeRepository;
public EmployeeService(IRepository<Employee> employeeRepository)
{
_employeeRepository = employeeRepository;
}
...
// Relevant methods that deal with operations on employees.
...
使用DropCreateDatabaseAlways
和一些种子数据初始化TestDatabaseContext
我有两个问题(第二个与第一个相关):
MySQL
&MSSQL
)为什么不模拟存储库而不是使用测试数据库呢?据我所知,您不应该在集成测试中模拟,因为您正在验证系统的组件是如何交互的。我相信与外部依赖(数据库)的交互也很重要。此外,我已经在单元测试中模拟了存储库。true。只是你有EF问题。我想说它不会刷新数据库,因为你仍然在同一个应用程序域中,从testsas到隔离,我见过有人推荐数据库快照。这些允许您创建具有唯一名称的数据库副本。可能只是MSSQL
[TestFixture]
public class EmployeeServiceTests
{
// System under test.
private EmployeeService _sut;
[SetUp]
public void SetUp()
{
_sut = new EmployeeService(
new Repository<Employee,TestDatabaseContext>(
new TestDatabaseContext()));
}
...
// Tests.
...