Reputation: 14771
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?
Upvotes: 0
Views: 540
Reputation: 29207
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();
Upvotes: 1