Question

I am learning the Moles framework for unit testing. I have a template ASP MVC project with AccountController and the method I want to test is Register()

    [HttpPost]
    public ActionResult Register(RegisterModel model)
    {
        if (ModelState.IsValid)
        {
            MembershipCreateStatus createStatus;
            Membership.CreateUser(model.UserName, model.Password, model.Email, null, null, false, null, out createStatus);
            if (createStatus == MembershipCreateStatus.Success)
            {
                string confirmationGuid = Membership.GetUser(model.UserName).ProviderUserKey.ToString();
                string confirmUrl = System.Web.HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) +
                    "/account/confirm?id=" + confirmationGuid;
                var message = new MailService.MessageModel
                                  {
                                      UserName = model.UserName,
                                      MessageSubject = "Registration confirmation",
                                      MessageBody = "Please follow the link below in order to activate your account:\n" + confirmUrl
                                  };
                MailService.SendConfrimationEmail(message);
                return RedirectToAction("Confirmation", "Account");
            }
            else
            {
                ModelState.AddModelError("", ErrorCodeToString(createStatus));
            }
        }
        return View(model);
    }

It depends on a static method Membership.CreateUser. The test method looks like this:

    [TestMethod()]
    [HostType("Moles")]
    public void RegisterTest()
    {
        using (MolesContext.Create())
        {                
            var target = new AccountController();
            var model = new RegisterModel
            {
                UserName = "TestUser1",
                Password = "12345678",
                ConfirmPassword = "12345678",
                Email = "mail@mail.com"
            };
            target.Register(model);
            var exists = Membership.GetUser("TestUser1") != null;
            Assert.IsTrue(exists);
        }
    }

I expect the Moles framework to mock the static method inside the Register method but while debugging I found that createStatus always has InvalidAnswer value hence the test fails. How can I accomplish this by using Moles framework without creating any interfaces or delegates manually?

Was it helpful?

Solution

I've finally found a way to mock Membership.CreateUser method, although the implementation is a bit long.

MMembership.CreateUserStringStringStringStringStringBooleanObjectMembershipCreateStatusOut = 
   (string userName, string password, string email, string passwordQuestion, 
    string passwordAnswer, bool isApproved, object providerUserKey,
    out MembershipCreateStatus @createStatus) =>
{
    @createStatus = MembershipCreateStatus.Success;
    return null;
};
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top