如何解决ASP.NET 模拟错误对象引用未设置为对象的实例
在 ASP.NET 中运行测试时出现错误。我所有的其他测试工作正常。这是我在控制器中的代码:
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,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,User.Identity.Name);
}
_treatmentRepository.AddTreatment(newTreatment,treatmentViewModel.animal.ID);
return View("Thanks");
}
else
{
return View(treatmentViewModel);
}
}
这是我在模拟测试中的代码。在这里我看看如果我创建一个操作处理它是否会发表评论。
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,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,vol.Name),Times.Once);
}
当我运行这段代码时,我在这一行收到错误:if (treatmentViewModel.animal.PassingAwayDate != null)
,它说这是 null
。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。