gpt4 book ai didi

c# - Moq - 尝试测试 ActionResult 时出现空引用异常

转载 作者:行者123 更新时间:2023-11-30 22:01:38 24 4
gpt4 key购买 nike

我正在尝试使用 Moq 学习单元测试,但我一直遇到同样的问题。我正在为我的 Controller ActionResults 编写单元测试,并且在创建我的 Controller 实例并测试它是否工作时没有问题。但是我有一个 ActionResult 给我一个空引用异常,因为我在调用 Controller 之前设置测试时遗漏了一些东西。

     private PersonController _controller;
private Mock<ICoreRepository> _repoCore;
private Mock<IImageRepository> _repoImage;

[TestInitialize]
public void Setup()
{
_repoCore = new Mock<ICoreRepository>();
_repoImage = new Mock<IImageRepository>();

_controller = new PersonController(_repoImage.Object)
{
CrmCoreRepository = _repoCore.Object,
//ImageRepository = _repoImage.Object
};
}

[TestMethod]
public void UploadImage_NotNull_personImage_Test()
{

var controller = new PersonController(_repoImage.Object);
var model = new GlobalEntityModel
{
PersonModel= new PersonModel()
};

controller.CrmCoreRepository = _repoCore.Object;
var uploadedFile = new Mock<HttpPostedFileBase>();

uploadedFile
.Setup(f => f.ContentLength)
.Returns(10);

uploadedFile
.Setup(f => f.FileName)
.Returns("testimage.jpg");

var imageRepository = new Mock<IImageRepository>();
imageRepository.Setup(x => x.SaveImage(It.IsAny<HttpPostedFileBase>(), It.IsAny<ImagePaths>()));

var res = controller.AddOrUpdatePersonDetail(model, uploadedFile.Object);

Assert.IsNotNull(res);
}

在我的测试中,我创建了一个新的 Controller 实例、模型、httppostedfilebase 的模拟,并且我尝试设置图像存储库 saveImage。

[HttpPost]
public ActionResult AddOrUpdatePersonDetail(GlobalEntityModel model, HttpPostedFileBase personImage)
{
var uploadedImage = string.Empty;
var personImagePath = _imageRepository.RetrieveImageFilePath(ImagePaths.PersonImageFilePath);

if (personImage != null)
{
uploadedImage = _imageRepository.SaveImage(personImage, ImagePaths.PersonImageFilePath);
}

var personModel = model.PersonModel;
var personDto = Mapper.MapTo<PersonDto>(personModel);

//Update Person
personDto.CreatedBy = "Admin";
personDto.UpdatedBy = "Admin";
personDto.UpdatedOn = DateTime.Now;

if (uploadedImage != string.Empty)
{
personDto.PersonImageRef = uploadedImage;

if (personDto.PersonImageRef.Contains(personImagePath))
{
personDto.PersonImageRef = personDto.PersonImageRef.Replace(personImagePath, string.Empty);
}
if (personDto.PersonImageRef.Contains("~/Content"))
{
personDto.PersonImageRef = string.Empty;
}
}

if (personDto.Id == 0)
{
personDto.GlobalEntityGUID = model.GlobalEntityGUID;

personDto = CrmCoreRepository.AddPerson(personDto, null, null, null);

if (personDto != null)
{
personModel.GlobalEntityGUID = personDto.GlobalEntityGUID;

var personDetailModel = model.PersonDetailModel;
personDetailModel.PersonId = personDto.Id;

var personDetailDto = Mapper.MapTo<PersonDetailDto>(personDetailModel);
personDetailDto.CreatedBy = "Admin";
personDetailDto.UpdatedBy = "Admin";

personDetailDto = personDetailModel.Id == 0 ? CrmCoreRepository.AddPersonDetail(personDetailDto) : CrmCoreRepository.UpdatePersonDetail(personDetailDto);
}
}
else if (personDto.Id > 0)
{
personDto = CrmCoreRepository.UpdatePerson(personDto, null, null, null);

if (personDto != null)
{
personModel.GlobalEntityGUID = personDto.GlobalEntityGUID;

var personDetailModel = model.PersonDetailModel;
personDetailModel.PersonId = personDto.Id;

var personDetailDto = Mapper.MapTo<PersonDetailDto>(personDetailModel);
personDetailDto.CreatedBy = "Admin";
personDetailDto.UpdatedBy = "Admin";

personDetailDto = personDetailModel.Id == 0 ? CrmCoreRepository.AddPersonDetail(personDetailDto) : CrmCoreRepository.UpdatePersonDetail(personDetailDto);
}
}

return RedirectToRoute("EditEntity", new { controller = "GlobalEntity", action = "EditEntity", entityGuid = personModel.GlobalEntityGUID, entityType = "P" });
}

enter image description here

屏幕截图中显示的 personImagePath 为空。因为我没有从我的测试中传递参数,所以我不知道如何让它包含一个值。我对如何在单元测试中解决这个问题感到困惑。任何帮助都会很棒。

最佳答案

您正在使用 IImageRepository 的两个不同实例。您需要对注入(inject)到您正在测试的对象的相同对象进行 Setup 调用(PersonController - IImageRepository)。

要修复它,改变

var imageRepository = new Mock<IImageRepository>();
imageRepository.Setup(x => x.SaveImage(It.IsAny<HttpPostedFileBase>(),
It.IsAny<ImagePaths>()));

到(注意返回)

_repoImage.Setup(x => x.SaveImage(It.IsAny<HttpPostedFileBase>(),
It.IsAny<ImagePaths>())).Returns("~/Content");

关于c# - Moq - 尝试测试 ActionResult 时出现空引用异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27550358/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com