Tutorial :Assert.AreEqual failed on two identical MVC ViewModel results?



Question:

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 = "testUser@test.com"                                             }                          };            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.


Solution:1

Is AccountViewModel IEquatable? If not, C# will just use Object.ReferenceEquals to compare them. That will only be true if they both reference to the exactly same object (have the same property values is not good enough).


Solution:2

Did you override the Equals() and GetHashCode() methods for your AccountViewModel class?

example:

public override bool Equals(object obj)      {          if (obj == null)          {              return false;          }            if (Object.ReferenceEquals(this, obj))          {              return true;          }            if (this.GetHashCode() == obj.GetHashCode())          {              return true;          }            return false;      }        public override int GetHashCode()      {          return Id.GetHashCode();      }  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »