gpt4 book ai didi

c# - 如何测试这个业务逻辑

转载 作者:行者123 更新时间:2023-11-28 20:30:15 25 4
gpt4 key购买 nike

您好,如果有人可以帮助我进行单元测试,我将非常高兴Visual Studio 单元测试的业务逻辑..

我用谷歌“编辑”并检查了不同的单元测试方法,但我保留了找到我不需要的 Controller 的单元测试..我会最很高兴有人可以帮助我了解如何对该方法进行单元测试。

这是我的商务舱

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());

这是为了测试

[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.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);
}
}
}
}

关于c# - 如何测试这个业务逻辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30712516/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com