Mocking ASP.NET Membership RRS feed

  • Question

  • Hi all,

    I'm new in using Pex and Moles for unit testing.

    I want to perform unit test on my code which uses ASP.NET Membership, and call the Membership.GetUser(username) method.

    I tried to create a user with the following code, but it's throwing the error "The property or indexer 'System.Web.Security.Moles.MMembership.CreateUserStringString' cannot be used in this context because it lacks the get accessor


    My code looks like this:

        public  class SomeTest

            [TestMethod ]
            [HostType ("Moles" )]
            public  void  Add()
                BHttpContext  context = BHttpContext .SetCurrent();
                BIPrincipal  user = context.SetUser();
                BIIdentity  identity = user.SetIdentity();

                user.Roles.SetOne("Administrator" );

                identity.IsAuthenticated = true ;
                identity.Name = "username" ;

                MMembership .BehaveAsCurrent();

    When I run the test, I got the MoleNotImplementedException was unhandled by user code. Membership.GetUser(String) was not moled.

    Could anyone please help?

    Code samples would be appreciated.



    Wednesday, December 15, 2010 7:11 AM

All replies

  • Could you first provide the code sample where you use the membership API?
    Jonathan "Peli" de Halleux - Try Pex online at www.pexforfun.com!
    Monday, January 10, 2011 10:56 PM
  • ASP.NET MVC example (unfinished) attempting to test the DownloadForm() method in the controller with the DownloadForm_Get_DownloadFormViewIfUserIsAuthenticated() test method. We are attempting to get past the "User.Identity.IsAuthenticated" statement in the DownLoadForm() method. We get the same error as Jon Mahadi.

    Peli, can you help us?


    public class FormsController : Controller
            public ActionResult DownloadForm()
                DirectoryInfo DirInfo = new DirectoryInfo("c:/VendorWebSiteForms");
                FileInfo[] FormFiles = DirInfo.GetFiles();
                List<SelectListItem> FormFileList = new List<SelectListItem>();

                foreach (FileInfo AFile in FormFiles)
                    FormFileList.Add(new SelectListItem
                        Text = AFile.Name, //In the drop-down list
                        Value = AFile.Name //Passed to "ViewData"

                ViewData["FormFileSelection"] = FormFileList;
                if (User.Identity.IsAuthenticated) //Don't allow a non-logged-in user to access the forms!
                    return View();
                    return RedirectToAction("LogOn", "Account");

    public void DownloadForm_Get_DownloadFormViewIfUserIsAuthenticated()
                var controller = new FormsController();
                BHttpContext context;
                BIPrincipal user;
                BCache cache;
                BIIdentity TheIdentity;
                bool IsIdentityAuthenticated;
                //string TheName;

                context = BHttpContext.SetCurrent();
                cache = context.SetCache();
                user = context.SetUser();
                TheIdentity = user.SetIdentity();
                TheIdentity.IsAuthenticated = true;
                TheIdentity.Name = "Andrew";

                IsIdentityAuthenticated = user.Identity.IsAuthenticated;

                //MMembershipUser ThisUser = new MMembershipUser();
                //DateTime TheTime;
                //TheTime = ThisUser.CreationDateGet();
                //MMembership.CreateUserStringString("Allie", "Pa$$w0rd4321");

                ActionResult result = controller.DownloadForm();

                Assert.IsInstanceOfType(result, typeof(ViewResult));
                ViewResult viewResult = (ViewResult)result;

    Tuesday, February 1, 2011 10:12 PM