在ASP.NET MVC单元测试中验证Moq抛出错误的方法

时间:2016-05-04 18:49:47

标签: c# asp.net asp.net-mvc unit-testing moq

我正在开发一个ASP.NET MVC项目。在我的项目中,我正在进行单元测试。我正在使用Moq来模拟我的业务逻辑。但是我遇到了Moq的问题。特别是用mock.Verify方法。请参阅下面的方案。

这是我正在测试的动作

[HttpPost]
        public ActionResult Edit(CreateRegionVM model)
        {
            if(ModelState.IsValid)
            {
                Region region = new Region
                {
                    Id = model.Id,
                    Name = model.Name,
                    MmName = model.MmName,
                    Description = model.Description,
                    MmDescription = model.MmDescription,
                    GeoLocation = model.GeoLocation,
                    ImagePath = model.ImagePath
                };
                String imagePath = String.Empty;
                if(model.ImageFile!=null && model.ImageFile.ContentLength>0)
                {
                    imagePath = fileHelper.UploadFile(model.ImageFile, AppConfig.RegionImageDir,null);
                    if(String.IsNullOrEmpty(imagePath))
                    {
                        return new HttpStatusCodeResult(HttpStatusCode.InternalServerError);
                    }
                    else
                    {
                        //create thumb & delete old images - check the image operations
                        fileHelper.CreateThumb(imagePath, AppConfig.RegionImageDir, AppConfig.RegionMediumThumbWidth, AppConfig.RegionMediumThumbHeight, AppConfig.MediumThumbSuffix);
                        fileHelper.CreateThumb(imagePath, AppConfig.RegionImageDir, AppConfig.RegionSmallThumbWidth, AppConfig.RegionSmallThumbHeight, AppConfig.SmallThumbSuffix);

                        fileHelper.DeleteFile(model.ImagePath);
                        fileHelper.DeleteFile(fileHelper.GetImagePath(model.ImagePath, AppConfig.MediumThumbSuffix));
                        fileHelper.DeleteFile(fileHelper.GetImagePath(model.ImagePath, AppConfig.SmallThumbSuffix));
                        model.ImagePath = imagePath;
                    }
                }

                try
                {
                    regionRepo.Update(region);
                    TempData["message"] = "Region successfully edited";
                    TempData["class"] = AppConfig.FlashSuccessClass;
                    return RedirectToAction("Edit", new { id = model.Id });
                }
                catch
                {
                    return new HttpStatusCodeResult(HttpStatusCode.InternalServerError);
                }
            }
            return View("Create",model);
        }

这是我的测试功能

 [TestMethod]
        public void Edited_And_Redirected()
        {
            var postFile = new Mock<HttpPostedFileBase>();
            postFile.Setup(m => m.ContentLength).Returns(1);

            CreateRegionVM model = new CreateRegionVM
            {
                Id = 1,
                Name = "test",
                ImageFile = postFile.Object
            };

            Mock<IRegionRepo> regionMock = new Mock<IRegionRepo>();
            regionMock.Setup(m => m.Update(new Region())).Verifiable();
            Mock<IFileHelper> fileMock = new Mock<IFileHelper>();
            fileMock.Setup(m => m.UploadFile(model.ImageFile, It.IsAny<String>(), null)).Returns("upload_file_path");

            RegionController controller = new RegionController(regionMock.Object, fileMock.Object, 0);
            var unknownView = controller.Edit(model);
            regionMock.Verify();
            Assert.IsInstanceOfType(unknownView, typeof(RedirectToRouteResult), "Not redirected");
        }

正如您可以看到我的测试方法,我使用验证来确保调用regionRepo.Update方法。但是当我运行测试时它会给我这个错误。

  

Moq.MockVerification异常:以下设置未匹配:   IRegionRepo m =&gt; m =&gt; Update()

为什么会抛出此错误? moq的验证方法如何工作?

1 个答案:

答案 0 :(得分:1)

看看这一行:

regionMock.Setup(m => m.Update(new Region())).Verifiable();

它会将输入与new Region()进行比较,但很可能无论你传入的是什么,都不会在指称上等同于此。

如果Region不重要,请尝试

regionMock.Setup(m => m.Update(It.IsAny<Region>())).Verifiable();