I am trying to unit test a UpdateUser Controller that uses AutoMapping. Here is the code for the controller
UpdateUserController
private readonly IUnitOfWork _unitOfWork;
private readonly IWebSecurity _webSecurity;
private readonly IOAuthWebSecurity _oAuthWebSecurity;
private readonly IMapper _mapper;
public AccountController()
{
_unitOfWork = new UnitOfWork();
_webSecurity = new WebSecurityWrapper();
_oAuthWebSecurity = new OAuthWebSecurityWrapper();
_mapper = new MapperWrapper();
}
public AccountController(IUnitOfWork unitOfWork, IWebSecurity webSecurity, IOAuthWebSecurity oAuthWebSecurity, IMapper mapper)
{
_unitOfWork = unitOfWork;
_webSecurity = webSecurity;
_oAuthWebSecurity = oAuthWebSecurity;
_mapper = mapper;
}
//
// Post: /Account/UpdateUser
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult UpdateUser(UpdateUserModel model)
{
if (ModelState.IsValid)
{
// Attempt to register the user
try
{
var userToUpdate = _unitOfWork.UserRepository.GetByID(_webSecurity.CurrentUserId);
var mappedModel = _mapper.Map(model, userToUpdate);
**mappedModel will return null when run in test but fine otherwise (e.g. debug)**
_unitOfWork.UserRepository.Update(mappedModel);
_unitOfWork.Save();
return RedirectToAction("Index", "Home");
}
catch (MembershipCreateUserException e)
{
ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
}
}
return View(model);
}
and this is my Unit Test
UpdateUserControllerTest
[Fact]
public void UserRepository_Update_User_Success()
{
Controller = new AccountController(UnitOfWork, WebSecurity.Object, OAuthWebSecurity.Object, Mapper);
const string emailAsUserName = "user@username.com";
const string password = "password";
const string email = "email@email.com";
const string emailNew = "newEmail@email.com";
const string firstName = "first name";
const string firstNameNew = "new first name";
const string lastName = "last name";
const string lastNameNew = "new last name";
var updatedUser = new User
{
Email = emailNew,
FirstName = firstNameNew,
LastName = lastNameNew,
UserName = emailAsUserName
};
WebSecurity.Setup(
s =>
s.CreateUserAndAccount(emailAsUserName, password,
new { FirstName = firstName, LastName = lastName, Email = email }, false))
.Returns(emailAsUserName);
updatedUser.UserId = WebSecurity.Object.CurrentUserId;
UnitOfWork.UserRepository.Update(updatedUser);
UnitOfWork.Save();
var actualUser = UnitOfWork.UserRepository.GetByID(updatedUser.UserId);
Assert.Equal(updatedUser, actualUser);
var model = new UpdateUserModel
{
Email = emailAsUserName,
ConfirmEmail = emailAsUserName,
FirstName = firstName,
LastName = lastName
};
var result = Controller.UpdateUser(model) as RedirectToRouteResult;
Assert.NotNull(result);
}
I have a gut feel that when run in test mode, the mapper does not look at the mapper configuration that I have setup in Global.asax. Since the error only occur during the execution of unit test but not when running the website as is. I have created a IMappaer interface as a DI so I can mock it for testing purposes. I used Moq for Mocking and xUnit as a test framework, I have also installed AutoMoq which I have not used yet. Any idea? Thank you for looking at my lengthy post. Hope someone can help, been scratching my head for hours and reading up lots of posts.