1

我有一个使用 EFcore 的 ABPZero 框架实现的 API。根据文档中的建议,我已经在内存数据库中使用 sqlite 进行了集成测试。但是当我尝试使用 mock 对同一个 API 进行单元测试时,我感到很困惑[假设 mock 在执行方面更快]

我在该方法上参考了不同的存储库。我应该在测试用例上模拟服务还是存储库?

   public PagedResultDto<PatientSearchDTO> Search(PatientInpu 
                                            PatientInput)
              {     
             List<PatientSearchDTO> patientSearch = new 
            List<PatientSearchDTO>();
        int totalCount=0;
        try
        {   
            var filterText = PatientInput.Filter.Trim().ToLower();

            //Get the patients filtered usinng the filter text.
            var result = this._demoRepository.GetAll().Where(x =>
                                                       x.Dem_Firstname.ToLower().Contains(filterText)
                                                        );
            var treatment = _treatmentRepository.GetAll();                
            // Get the treatments for the pateints we fetched.
            var demotreatment = (from tdemo in result
                                 join treat in treatment on tdemo.Dem_Acu_Id equals treat.trm_acu_id into g1
                                 from treat in g1.DefaultIfEmpty()                                    
                                 select new
                                 {
                                     Acu_Id = tdemo.Dem_Acu_Id,
                                     FirstName = tdemo.Dem_Firstname.Trim(),
                                     Surname = tdemo.Dem_Surname.ToUpper().Trim() + (string.IsNullOrWhiteSpace(tdemo.Dem_Alias) ? string.Empty : " (" + tdemo.Dem_Alias.Trim() + ")"),
                                     MaidenName = tdemo.Dem_Maidname.Trim(),
                                     DOB = tdemo == null ? DateTime.MinValue : tdemo.Dem_Dob,
                                     RegistrationNumber = tdemo.Dem_RegNo == null ? string.Empty : tdemo.Dem_RegNo.Trim(),
                                     Photo = tdemo.Dem_Photo,                                         
                                     TreatmentStartDate = treat == null ? Convert.ToDateTime("1900-01-01 00:00:00.000") : Convert.ToDateTime(treat.trm_tr_startdate)
                                 });

这是测试的构造函数,

  public PatientServiceTests()
    {
        service = LocalIocManager.Resolve < IPatientService >();
        //creating set up data.
        SetUp();
    }

    private void SetUp()
    {
        //Arrange
        UsingIdeasDbContext(context =>
        {
            context.Demos.Add(new TDemo
            {
                Id = 1,
                Dem_Acu_Id = "1",
                Dem_Firstname = "Jack",
                Dem_Surname = "Jill",
                Dem_Maidname = "",
                Dem_Dob = DateTime.UtcNow.AddYears(-30),
                Dem_Alias = "JJ",
                Dem_RegNo = "123456"
            });
           context.TTreatment.Add(new TTreatment
            {
                trm_acu_id = "1",
                trm_cycle = "4207-1",
                trm_tr_startdate = DateTime.UtcNow.AddMonths(-1),
            });
    }

   [Fact]
    public void Should_SearchPatients()
    {
         PatientInpu input = new PatientInpu () { Filter = "ja", 
         Sorting = "FirstName ASC", SkipCount = 0, MaxResultCount = 10 };
        var result = service.Search(input);
        result.Items.Count.ShouldBe(1);
    }

对这种方法进行单元测试的最佳方法是什么?在 -memory 或 Mock 中?

4

1 回答 1

0

理想情况下,您应该在数据库实现之上有一个抽象层。您的调用者不需要知道实现细节。

话虽如此,通过接口公开数据库功能,并在单元测试时模拟接口/服务(使用可以使用 Moq 等模拟框架或编写自定义模拟)。

于 2019-08-20T19:42:00.530 回答