Hi I would really be happy if someone can help me with unit testing this business logic on Visual Studio unit test..
I've google"ed" and checked different ways of unit testing but I keep finding the unit tests for controllers which I don't need.. I'd be most happy somebody can help me know how to unit test this method.
This is my Business class
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);
}
}
}
This is my Repository Class
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;
}
}
You can make factories to create a different repository for test
Create two factories, one for test and one for production
Create two repositories. One for test and one for production
Use this for production
And this for test
EDIT
I forgot to show how to use the Factory. Send it as an parameter to the constructor, and then call Factory.Create()