I get a error when i run my test in asp.net. all my other tests works fine. This is my code in the controller:
public IActionResult CreateTreatment(TreatmentViewModel treatmentViewModel)
{
treatmentViewModel.animal = _animalRepository.GetById(treatmentViewModel.animal.ID);
if (treatmentViewModel.animal.PassingAwayDate != null)
{
ModelState.AddModelError(string.Empty, "This animal has passed away");
}
Animal newAnimal = _animalRepository.GetById(treatmentViewModel.animal.ID);
if (treatmentViewModel.animal.Age == 0)
{
//Animal newAnimal = _animalRepository.GetById(treatmentViewModel.animal.ID);
DateTime date1 = (DateTime)newAnimal.BirthDate;
var date2 = DateTime.Now;
var monthscalculated = (date1.Year - date2.Year) * 12 + date2.Month - date1.Month;
TimeSpan dayscalculated = date2 - date1;
var totaldays = dayscalculated.TotalDays;
if (monthscalculated < 6
&& ((int)treatmentViewModel.Treatment.TreatmentType == 0
|| (int)treatmentViewModel.Treatment.TreatmentType == 1)
&& treatmentViewModel.Treatment.AgeRequirement < 6 && totaldays < 180)
{
ModelState.AddModelError(string.Empty, "You cannot do this treatment");
}
}
if (ModelState.IsValid)
{
Treatment newTreatment = treatmentViewModel.Treatment;
if ((int)treatmentViewModel.Treatment.TreatmentType == 0 || (int)treatmentViewModel.Treatment.TreatmentType == 1)
{
{
var animal = _animalRepository.GetById(treatmentViewModel.animal.ID);
animal.NeuteredOrSterilized = true;
_animalRepository.UpdateById(animal);
}
}
if ((int)treatmentViewModel.Treatment.TreatmentType == 3)
{
Comment newComment = new Comment();
newComment.CommentString = "Operation";
CommentViewModel addCommentViewModel = new CommentViewModel();
addCommentViewModel.Comment = newComment;
addCommentViewModel.AnimalId = treatmentViewModel.animal.ID;
_commentRepository.AddComment(addCommentViewModel.Comment, addCommentViewModel.AnimalId, User.Identity.Name);
}
if ((int)treatmentViewModel.Treatment.TreatmentType == 5)
{
Comment newComment = new Comment();
newComment.CommentString = "Euthanasia";
CommentViewModel addCommentViewModel = new CommentViewModel();
addCommentViewModel.Comment = newComment;
addCommentViewModel.AnimalId = treatmentViewModel.animal.ID;
_commentRepository.AddComment(addCommentViewModel.Comment, addCommentViewModel.AnimalId, User.Identity.Name);
}
if ((int)treatmentViewModel.Treatment.TreatmentType == 4)
{
Comment newComment = new Comment();
newComment.CommentString = "Chipping GUID: " + Guid.NewGuid();
CommentViewModel addCommentViewModel = new CommentViewModel();
addCommentViewModel.Comment = newComment;
addCommentViewModel.AnimalId = treatmentViewModel.animal.ID;
_commentRepository.AddComment(addCommentViewModel.Comment, addCommentViewModel.AnimalId, User.Identity.Name);
}
_treatmentRepository.AddTreatment(newTreatment, treatmentViewModel.animal.ID);
return View("Thanks");
}
else
{
return View(treatmentViewModel);
}
}
This is the code I have in my mocking test. Here I am looking if it makes a comment if i create a operation treatment.
public void BR_4_Explanation_in_treatment_operation()
{
// Arrange
var loggerMock = new Mock<ILogger<HomeController>>();
var animalRepoMock = new Mock<IAnimalRepository>();
var residenceRepoMock = new Mock<IResidenceRepository>();
var treatmentRepoMock = new Mock<ITreatmentRepository>();
var clientapplicationRepoMock = new Mock<IClientAnimalApplicationRepository>();
var commentRepoMock = new Mock<ICommentRepository>();
var shoppingCartMock = new Mock<IShoppingCartRepository>();
var clientMock = new Mock<IClientRepository>();
var controller = new AnimalController(loggerMock.Object, animalRepoMock.Object, treatmentRepoMock.Object,
residenceRepoMock.Object,
commentRepoMock.Object, clientapplicationRepoMock.Object, clientMock.Object, shoppingCartMock.Object);
Residence residence = new Residence()
{
ID = 1,
Capacity = 5,
CatOrDogResidence = "Dog",
Neutered = true,
InvidualOrGroup = "group",
Sex = "Male"
};
Volunteer vol = new Volunteer()
{
ID = 1,
Email = "ddjkdkd@hotmail.com",
Name = "barend",
DateOfBirth = DateTime.Now,
PhoneNumber = "dddd"
};
Animal an = new Animal()
{
ID = 1,
Adoptable = true,
Description = "hi",
Name = "doggo",
DogOrCat = "Dog",
ReasonAway = "hi",
NeuteredOrSterilized = true,
Photo = "gg",
Sex = "Male",
WithChildrenOrNot = "no",
IncomingDate = DateTime.Now,
EstimatedAge = 20,
Residence = residence,
Age = 20
};
Treatment treatment = new Treatment()
{
Animal = an,
AgeRequirement = 5,
Costs = 30,
DateOfTreatment = DateTime.Now,
Description = "test",
ID = 1,
TreatedBy = "barend",
TreatmentType = TreatmentType.Operation
};
TreatmentViewModel treatview = new TreatmentViewModel()
{
animal = an,
Treatment = treatment
};
Comment comment = new Comment()
{
CommentString = "Operation",
Animal = an,
CommentMadeBy = vol,
ID = 1,
Date = DateTime.Now
};
//animalRepoMock.Setup(animal => animal.AddAnimal(an)).Returns(an);
// treatmentRepoMock.Setup(tr => tr.AddTreatment(treatment, an.ID));
commentRepoMock.Setup(com => com.AddComment(comment, an.ID, "barend"));
//act
controller.CreateTreatment(treatview);
//assert
commentRepoMock.Verify(ps => ps.AddComment(comment, an.ID, vol.Name), Times.Once);
}
When I run this code I get the error on this line: if (treatmentViewModel.animal.PassingAwayDate != null), it says this is null.
Aucun commentaire:
Enregistrer un commentaire