c# - Using Moles framework to mock static dependencies -


I am learning the Moles framework for unit testing, and I have a template ASP MVC project method with the AccountController to test. Register is wanted () [HttpPost] public ActionResult Register (RegisterModel Models) {If (ModelState.IsValid) {membership CreateStatus installation site; Membership Buyer user (model username, model password, model email, empty, empty, incorrect, blank, create status); If (createStatus == MembershipCreateStatus.Success) {string confirmationGuid = Membership.GetUser (model.UserName) .ProviderUserKey.ToString (); String confirmUrl = System.Web.HttpContext.Current.Request.Url.GetLeftPart (UriPartial.Authority) + "/ Account / Confirmation ID =?" + ConfirmationGuid; Message above = New MailService.MessageModel {username = model.UserName, MessageSubject = "Registration Confirmation", MessageBody = "Please activate your account for the following link: \ n" + confirmUrl}; MailService.SendConfrimationEmail (message); Return Redirect Tax ("Confirmation", "Account"); } Other {ModelState.AddModelError ("", ErrorCodeToString (createStatus)); }} View Return (Model); }

This is a static method membership. CreateUser depends on . Exam method looks like this:

 [TestMethod ()] [HostType ("Moles")] Public Zero RegisterTest (using {{MolesContext.Create ()} {var Target = New AccountController (); Var Model = New RegisterModel {user name = "testuser1", password = "12345678", confirm password = "12345678", email = "mail@mail.com"}; Target.Register (model); Var exists = Membership GETUser ("TestUser1")! = Null; Assert.IsTrue (exists); }}   

I hope that the Moles frame should be duplicated with the static method within the register method, but while debugging I found that createStatus < / Code> is always invalid answer value so the test fails How can I do this by using Moles Framework without making any interface or delegate manually?

I finally found a way to ridicule Membership.CreateUser The method is, though the implementation is a bit longer.

  MMembership.CreateUserStringStringStringStringStringBooleanObjectMembershipCreateStatusOut = (string user name, string password, string email, string password question, string passwordover, boot disabled, object provider user, out membership credits @CratsStatus) = & gt; {@createStatus = MembershipStats Success; Return tap; };    

Comments