如何测试此业务逻辑

本文关键字:业务 测试 何测试 | 更新日期: 2023-09-27 17:54:11

嗨,如果有人能帮助我进行单元测试,我真的很高兴业务逻辑在Visual Studio单元测试…

我谷歌了"ed",检查了不同的单元测试方法,但我保留了找到我不需要的控制器的单元测试…我是最很高兴有人能帮助我知道如何单元测试这个方法。

这是我的商务舱

public void AddConsultation(ConsultationView cv, int patientid)
{
    using (var deprepo = new ConsultationRepository())
    {
        if (cv.ConsultId == 0)
        {
            var curr = DateTime.Now;
            string date = curr.ToString("d");
            string  time= curr.ToString("t");
            var patient = da.Patients.ToList().Find(x => x.PatientId == patientid);
            Consultation _consultation = new Consultation
            {
              ConsultId = cv.ConsultId,
              ConsultDate = date,
              ConsultTime = time,
              illness = cv.illness,
              PresribedMed = cv.PresribedMed,
              Symptoms = cv.Symptoms,
              U_Id = patient.PatientId,
            };
            deprepo.Insert(_consultation);
        }
    }
}

这是我的存储库类

public class ConsultationRepository:IConsultationRepository
{
    private DataContext _datacontext = null;
    private readonly IRepository<Consultation> _clinicRepository;
    public ConsultationRepository()
    {
        _datacontext = new DataContext();
        _clinicRepository = new RepositoryService<Consultation>(_datacontext);
    }
    public Consultation GetById(int id)
    {
        return _clinicRepository.GetById(id);
    }
    public List<Consultation> GetAll()
    {
        return _clinicRepository.GetAll().ToList();
    }
    public void Insert(Consultation model)
    {
        _clinicRepository.Insert(model);
    }
    public void Update(Consultation model)
    {
        _clinicRepository.Update(model);
    }
    public void Delete(Consultation model)
    {
        _clinicRepository.Delete(model);
    }
    public IEnumerable<Consultation> Find(Func<Consultation, bool> predicate)
    {
        return _clinicRepository.Find(predicate).ToList();
    }
    public void Dispose()
    {
        _datacontext.Dispose();
        _datacontext = null;
    }
}

如何测试此业务逻辑

您可以创建工厂来为测试创建不同的存储库

//Interface for a factory class
public interface IFactory
{
    IIConsultationRepository Create();
}

创建两个工厂,一个用于测试,一个用于生产

public class MyFactory : IFactory
{
    public IIConsultationRepository Create()
    {
        return new ConsultationRepository();
    }
}
public class MyTestFactory : IFactory
{
    public IIConsultationRepository Create()
    {
        return new ConsultationTestRpository();
    }
}

创建两个存储库。一个用于测试一个用于生产

public class ConsultationTestRpository : IConsultationRepository
{
    //Your test repository. In this you skip the database.
    //This is just one simple example of doing it.
    Consultation _consultation;
    public Consultation GetById(int id)
    {
        return _consultation;
    }

    public void Insert(Consultation model)
    {
        _consultation = model;
    }
}
public class ConsultationRepository : IConsultationRepository
{
    //Your repository
}

用于生产

var obj = new TheConsultationClass(new MyFactory());

这是test

[TestClass]
public class UnitTest1
{
    [TestMethod]
    public void TestMethod1()
    {
        var objForTesting = new TheConsultationClass(new MyTestFactory());
        var consultationView = new ConsultationView();

        objForTesting.AddConsultation(consultationView, 123);
        var consultation = objForTesting.GetById(...);
        Assert.AreEqual(123, consultation.U_Id );
    }
}

编辑

我忘记展示如何使用Factory了。将其作为参数发送给构造函数,然后调用Factory.Create()

public class TheConsultationClass
{
    public MyFactory Factory { get; set; }
    public TheConsultationClass(IFactory factory)
    {
        Factory = factory;
    }
    public void AddConsultation(ConsultationView cv, int patientid)
    {
        using (var deprepo = Factory.Create())
        {
            if (cv.ConsultId == 0)
            {
                var curr = DateTime.Now;
                string date = curr.ToString("d");
                string time = curr.ToString("t");
                var patient = da.Patients.ToList().Find(x => x.PatientId == patientid);
                Consultation _consultation = new Consultation
                {
                    ConsultId = cv.ConsultId,
                    ConsultDate = date,
                    ConsultTime = time,
                    illness = cv.illness,
                    PresribedMed = cv.PresribedMed,
                    Symptoms = cv.Symptoms,
                    U_Id = patient.PatientId,
                };
                deprepo.Insert(_consultation);
            }
        }
    }
}

如果您阅读了测试控制器的示例并理解了它们,那么测试普通的类和方法应该是相当简单的。它基本上是相同的,但是你对你的类结构有更多的控制。

实际上,您的代码存在一些问题,使其难以测试。让我们看看你现有的代码:

public void AddConsultation(ConsultationView cv, int patientid)
{

下面这行创建了一个新的consultanrepository。这很难模拟,这意味着很难检查是否正确调用了存储库。更好的方法是通过类的构造函数将存储库或工厂注入到类中。这可以手工完成,正如@AxdorphCoder所描述的,或者你可以使用IOC容器(如CastleWindsor或Ninject)为你做一些繁重的工作。

    using (var deprepo = new ConsultationRepository())
    {
        if (cv.ConsultId == 0)
        {

下一行引用了DateTime.Now。同样,这很难测试。同样,如果需要知道使用了某个特定日期,最简单的解决方案是要么为该日期注入工厂,要么注入该日期。如果不需要精确,另一种方法是在测试开始时存储时间,然后验证所使用的时间是在测试开始和测试断言之间的时间。

            var curr = DateTime.Now;
            string date = curr.ToString("d");
            string  time= curr.ToString("t");

下一行引用da。在您给出的代码示例中,它没有被初始化。看起来像是另一个仓库…在这种情况下,上面关于注入存储库的建议仍然有效。顺便问一句,如果找不到病人怎么办?

            var patient = da.Patients.ToList().Find(x => x.PatientId == patientid);
            Consultation _consultation = new Consultation
            {
              ConsultId = cv.ConsultId,
              ConsultDate = date,
              ConsultTime = time,
              illness = cv.illness,
              PresribedMed = cv.PresribedMed,
              Symptoms = cv.Symptoms,
              U_Id = patient.PatientId,
            };
            deprepo.Insert(_consultation);
        }
    }
}

那么,您将如何测试AddConsultant方法呢?让我们假设您将遵循AAA模式进行测试安排。下面是一些您可以编写的测试:

  1. 如果ConsultId已设置,则Validate Repository未更新

    Arrange - Setup mock repository
            - setup da.Patients to contain Patient with correct id 
            - Create system under test injecting repository
            - Create view, with ConsultId <> 0
    Act     - Call sut.AddConsultation(view, somePationId)
    Assert  - No methods called on injected repository
    
  2. 用View中的值创建验证咨询

    Arrange - Create View to pass, containing consultId = 0
            - setup da.Patients to contain Patient with correct id 
              Store datetime test started
              Create mock Repository and set it up to expect a call to insert
              Create system under test and Inject respository
    Act     - Call sut.AddConsultation(view, somePationId)
    Assert  - Assert Insert on respository was called with a consultation
              Assert each property of consulation has expected values from Arrange
              Assert consultation date is >= Arrange Date and <= Now
    
  3. 如果没有找到患者,验证AddConsultation失败

    Arrange - Create View to pass, containing consultId = 0
            - setup da.Patients, that doesn't contain Patient with correct id 
              Create mock Repository and set it up to not expect call
              Create system under test and Inject respository
    Act     - Call sut.AddConsultation(view, somePationId)
    Assert  - Assert exception thrown with correct information (currently not done)