0

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.

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()

Why is this error thrown? How does the verify method of moq work?

1 Answer 1

1

Take a look at this line:

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

It's going to compare the input to new Region(), but most likely whatever you're passing in is not going to be referentially equal to that.

If the Region doesn't matter, try

regionMock.Setup(m => m.Update(It.IsAny<Region>())).Verifiable();
Sign up to request clarification or add additional context in comments.

3 Comments

@Wai The It.IsAny<T> basically wires it up so that any region that is passed to it will be part of the mock implementation, and therefore verifiable.
So, verify is roughly used for comparing output? If void method, we can just verify and mock. Is it?
Verify is used to check that methods got called. Verifiable indicates that you intend to call Verify. If the parameters matter then you can specify specific parameters. But if they don't matter then you would use It.IsAny.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.