I am new to unit testing. I am trying to test something very simple:
[HttpPost]
public ActionResult EditProfile(ProfileViewModel model)
{
if (ModelState.IsValid)
{
// Retrieve current user
var userId = User.Identity.GetUserId();
var user = _dataRepository.GetUserById(userId);
//If it isn't the single-instance default picture, delete the current profile
// picture from the Profile_Pictures folder
if (!String.Equals(user.ProfilePictureUrl, _defaultPic))
System.IO.File.Delete(Server.MapPath(user.ProfilePictureUrl));
In this section of code, I am creating a condition where this line will evaluate as true:
if (!String.Equals(user.ProfilePictureUrl, _defaultPic))
I would like to verify that System.IO.File.Delete
is called.
What is the best way to do this?
Do I need to refactor by wrapping the System.IO.File.Delete
call in my own class that implements an interface so that I can mock it and verify that it was called?
I am using Moq.
Service.MapPath
Those are implementation concerns that can be abstracted out. Actually that whole statement can be encapsulated in one abstraction. – Nkosi