【问题标题】:Verify method of Moq throwing error in ASP.NET MVC unit testing验证 ASP.NET MVC 单元测试中 Moq 抛出错误的方法
【发布时间】:2016-05-04 18:49:46
【问题描述】:

我正在开发一个 ASP.NET MVC 项目。在我的项目中,我正在进行单元测试。我正在使用 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");
        }

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

Moq.MockVerification 异常:以下设置不匹配: IRegionRepo m=>m=>Update()

为什么会抛出这个错误?起订量的验证方法是如何工作的?

【问题讨论】:

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


    【解决方案1】:

    看看这一行:

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

    它会将输入与new Region() 进行比较,但很可能您传入的任何内容都不会在引用上与之相等。

    如果Region 没关系,试试

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

    【讨论】:

    • @Wai It.IsAny&lt;T&gt; 基本上将其连接起来,以便传递给它的 任何 区域将成为模拟实现的一部分,因此是可验证的。
    • 那么,verify大概是用来比较输出的吧?如果是 void 方法,我们可以验证和模拟。是吗?
    • Verify 用于检查方法是否被调用。 Verifiable 表示您打算调用Verify。如果参数很重要,那么您可以指定特定参数。但如果它们无关紧要,那么您将使用It.IsAny
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-14
    • 1970-01-01
    • 1970-01-01
    • 2020-10-28
    • 1970-01-01
    • 2011-05-09
    相关资源
    最近更新 更多