Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 具有多个存储库的集成测试服务_C#_Entity Framework_Testing_Nunit - Fatal编程技术网

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.
    
    ...