Hi I am trying to set up a unit test that has reference of membership.provider in it. This is done in MVC3.
I tried doing a couple things but I got no luck.
On this accountcontroller, the Login Method goes through a large amount of logic.
I tried this:
where fakeprovider was Mock.Create<CustomMembershipProvider>();
It is always returning as null.
I was able to get my unit test to make sure that it can check the logic of the !ModelState.IsValid, but I cannot seem to get the two bolded lines in the code below to return what I want it to return.
I tried to use the doinstead but I had to make the Arrange/ArrangeSet in a try/catch block.
Is there something that I am missing that is making this always null.
Any help would be greatly appreciate. Thanks.
I tried doing a couple things but I got no luck.
On this accountcontroller, the Login Method goes through a large amount of logic.
I tried this:
Mock.Arrange(() => Membership.Provider).Returns(fakeProvider);
where fakeprovider was Mock.Create<CustomMembershipProvider>();
It is always returning as null.
I was able to get my unit test to make sure that it can check the logic of the !ModelState.IsValid, but I cannot seem to get the two bolded lines in the code below to return what I want it to return.
I tried to use the doinstead but I had to make the Arrange/ArrangeSet in a try/catch block.
Is there something that I am missing that is making this always null.
Any help would be greatly appreciate. Thanks.
public ActionResult Login(AccountLogin accountLogin) if (!ModelState.IsValid) { return View(accountLogin); } CustomMembershipProvider membershipProvider = Membership.Provider as CustomMembershipProvider; MembershipUser user = membershipProvider.GetUser(accountLogin.UserName); if (user != null && user.LastLoginDate.AddDays(180) < DateTime.Now)
{
membershipProvider.LockUser(user.UserName); ModelState.AddModelError("", inactivityLockedMessage); return View(accountLogin); } if (Membership.ValidateUser(accountLogin.UserName, AESCryptography.Encrypt(accountLogin.Password))) { // store single session ID user.Comment = Guid.NewGuid().ToString(); Membership.UpdateUser(user); public void Login_Where_ModelState_IsNot_Valid_test() //this unit test passes with no issue
{ var target = new AccountController(); var accountLogin = new AccountLogin(); var result = new ViewResult(); Mock.Arrange(() => target.Login(accountLogin)).OccursOnce(); //to make target.ModelState.IsValid return false target.ModelState.AddModelError("key", "needs to be false"); result = (ViewResult)target.Login(accountLogin); Assert.IsTrue(target.ModelState.IsValid == false); Assert.IsTrue(result.ViewName == string.Empty); Mock.Assert(target.Login(accountLogin)); }
[TestMethod]
public void Login_Where_Inactivity_is_Hit_test() { string inactivityLockedMessage = "Your account is locked due to inactivity. Please contact AI Portal support."; //Arrange controller = Mock.Create<AccountController>(); accountLogin = new AccountLogin { UserName = "" } //string.empty; action = Mock.Create<ActionResult>();
Mock.Arrange(() => controller.ModelState.IsValid).Returns(true); CustomMembershipProvider fakeProvider = Mock.Create <CustomMembershipProvider>(); MembershipUser user = Mock.Create<MembershipUser>(); bool called = false;
try { Mock.ArrangeSet(() => action = controller.Login(Arg.IsAny<AccountLogin>())) .DoInstead(() => { Mock.ArrangeSet(() => user.LastLoginDate.AddDays(180)); Mock.Arrange(() => user.LastLoginDate < DateTime.Now).Returns(true); controller.ModelState.AddModelError("", inactivityLockedMessage); called = true; }) .Returns(action); } catch (NullReferenceException) { Mock.Arrange(() => fakeProvider.LockUser(accountLogin.UserName)).Returns(true); Mock.ArrangeSet(() => user.LastLoginDate.AddDays(180)); Mock.Arrange(() => user.LastLoginDate < DateTime.Now).Returns(true); controller.ModelState.AddModelError("", inactivityLockedMessage); called = true; } //Assert Mock.Assert(user.LastLoginDate); Mock.Assert(fakeProvider); Assert.IsTrue(called); Assert.IsFalse(controller.ModelState.IsValid); Assert.IsTrue(controller.ModelState.Count==1, "should not be more than 1 error"); string modelerror = controller.ModelState[""].Errors[0].ErrorMessage; Assert.IsTrue(inactivityLockedMessage==modelerror, "should be equal"); }