如果有人可以帮我进行单元测试,我真的很高兴 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;
}
}
答案 0 :(得分:2)
您可以让工厂为测试
创建不同的存储库//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);
}
}
}
}
答案 1 :(得分:0)
如果您正在阅读测试控制器的示例并理解它们,那么测试正常的类和方法应该非常简单。它基本相同,但你可以更好地控制你的类结构。
目前,您的代码存在一些难以测试的问题。让我们看看你现有的代码:
public void AddConsultation(ConsultationView cv, int patientid)
{
下面一行创建一个新的ConsultantRepository。这很难模拟,这意味着很难检查是否正确调用了存储库。更好的方法是通过其构造函数将存储库或工厂注入类。如@AxdorphCoder所述,这可以手动滚动,或者你可以使用像CastleWindsor或Ninject这样的IOC容器为你做一些繁重的工作。
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 pattern for test arrangement。以下是您可能会写的一些测试:
如果已经设置了ConsultId,则不会更新验证存储库
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
验证使用视图中的值创建的咨询
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
如果找不到患者,则验证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)