I got this error on my unit test:
Assert.AreEqual failed. Expected:<ShizoMe.Web.ViewModel.AccountViewModel>. Actual:<ShizoMe.Web.ViewModel.AccountViewModel>.
This is the code for my test:
[TestMethod]
public void Register_Prevents_Duplicate_Users()
{
var controller = GetAccountController();
var model = new AccountViewModel
{
Register = new RegisterModel
{
EmailAddress = "[email protected]"
}
};
var result = (ViewResult) controller.Register(model.Register);
Assert.AreEqual("A user with this email address already exists.",
controller.ModelState["UserExists"].Errors[0].ErrorMessage);
Assert.AreEqual(model, result.ViewData.Model);
}
This is my Register method:
[HttpPost]
public virtual ActionResult Register([Bind(Prefix = "Register")]RegisterModel model)
{
var accountModel = new AccountViewModel();
if (ModelState.IsValid)
{
if (_accountRepository.GetUser(model.EmailAddress) != null)
{
ModelState.AddModelError("UserExists", "A user with this email address already exists.");
return View(accountModel);
}
var newUser = new User
{
EmailAddress = model.EmailAddress,
Password = model.Password,
CreatedDate = DateTime.UtcNow
};
if (_accountRepository.RegisterUser(newUser))
{
_formsService.SignIn(newUser);
return RedirectToAction(MVC.Home.Index());
}
}
return View(accountModel);
}
Any idea why the last Assert.AreEqual failed? When I debug, the ViewModels (model and result.ViewData.Model) are the same.
Thank you very much.