Intercepting Membership.Provider

2 posts, 0 answers
  1. Ewin
    Ewin avatar
    13 posts
    Member since:
    Jan 2012

    Posted 22 Oct 2012 Link to this post

    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:

    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)
    ModelState.AddModelError("", inactivityLockedMessage);
    return View(accountLogin);
    if (Membership.ValidateUser(accountLogin.UserName, AESCryptography.Encrypt(accountLogin.Password)))
      // store single session ID
    user.Comment = Guid.NewGuid().ToString();
    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);





    public void Login_Where_Inactivity_is_Hit_test()
        string inactivityLockedMessage = "Your account is locked due to inactivity.  Please contact AI Portal support.";
        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
         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;
         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.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");  

  2. Kaloyan
    Kaloyan avatar
    878 posts

    Posted 25 Oct 2012 Link to this post

    Hi Ewin,

     Thank you for reaching Telerik support.

     In order to help you further, I must object that you are using the JustMock Free edition, which does not support features using profiler. Without the profiler you are not able to mock static members/classes, which makes the correct execution of your test method difficult. You can find more about the Free and Commercial version of our product here:

     If there is anything else, I can help you with, please do not hesitate to ask.

    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Back to Top