mercredi 4 mai 2016

Verify method of Moq throwing error in ASP.NET MVC unit testing

I am developing an ASP.NET MVC project. In my project I am doing unit testing. I am using Moq to mock my business logics. But I am having a problem with Moq. Especially with mock.Verify method. Please see my scenario below.

This is the action I am testing

[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);
        }

This is my test function

 [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");
        }

As you can see my test method, I am using verify to make sure regionRepo.Update method is called. But it is giving me this error when I run the test.

Moq.MockVerification Exception: The following setups were not matched: IRegionRepo m=>m=>Update()

So please help me why this error thrown? Please make me sure how verify method of moq work? Please explain me about it? Please help me. Thanks.

Aucun commentaire:

Enregistrer un commentaire