嗨,如果有人能帮我在Visual单元测试上测试这个业务逻辑,我真的很高兴。
我搜索了“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;
}
}发布于 2015-06-08 15:16:55
您可以让工厂创建不同的存储库进行测试。
//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);
}
}
}
}发布于 2015-06-08 22:37:50
如果您阅读并理解了测试控制器的示例,那么测试普通类和方法应该是非常简单的。这基本上是一样的,但您可以更好地控制您的类结构。
就目前情况而言,您的代码存在一些问题,使其难以测试。让我们看看您现有的代码:
public void AddConsultation(ConsultationView cv, int patientid)
{下面的行创建一个新的ConsultantRepository。这很难模拟,这意味着很难检查存储库是否被正确调用。更好的方法是通过构造函数将存储库或工厂注入到类中。可以像@AxdorphCoder所描述的那样手工滚动,也可以使用IOC容器(如CastleWindsor或尼尼特 )为您做一些繁重的工作。
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模式。下面是一些您可以编写的测试:
https://stackoverflow.com/questions/30712516
复制相似问题