Note that there are some explanatory texts on larger screens.

plurals
  1. PONeed Help understanding this code
    primarykey
    data
    text
    <p>I am trying to learn unit testing. I am trying to unit test some Memembership stuff I am making in asp.net mvc 1.0. I been following a book on MVC and I am confused about some stuff that hopefully someone can clear up for me.</p> <p>I am using Nunit and Moq for my frameworks.</p> <p>Question 1:</p> <pre><code> public AuthenticationController(IFormsAuthentication formsAuth, MembershipProvider provider) { FormsAuth = formsAuth ?? new FormsAuthenticationWrapper(); Provider = provider ?? Membership.Provider; } </code></pre> <p>I am kinda confused what "??" does I never really seen it before. Like I don't even know whats happening really in here. Like they passin the interface and then "??" mark happens and makes a new FormsAuthenticationWraper is made?</p> <p>Question 2.</p> <pre><code> public AuthenticationController(): this(null, null) { } </code></pre> <p>I know this is the default constructor but I am not sure why ": this(null,null)" is doing.</p> <p>Like what is it implementing? and what is this refering too. And on top of it why can't that be just left out? And just stick the default constructor as it is.</p> <p>Question 3.</p> <p>In the book(asp.net mvc 1.0 quickly) it talks about how it would be quite a bit of work to implementing the Memembership provider would be alot of work. So they use moq mockup framework to make life easier.</p> <p>Now my question is they don't use the moq on the "FormsAuthentication". They instead make an interface</p> <pre><code> public interface IFormsAuthentication { void SetAuthCookie(string userName, bool createPersistentCookie); void SignOut(); } </code></pre> <p>Then make a wrapper</p> <p>public class FormsAuthenticationWrapper : IFormsAuthentication { public void SetAuthCookie(string userName, bool createPersistentCookie) { FormsAuthentication.SetAuthCookie(userName, createPersistentCookie); } public void SignOut() { FormsAuthentication.SignOut(); }</p> <pre><code>} </code></pre> <p>Then finally a property</p> <pre><code> public IFormsAuthentication FormsAuth { get; private set; } </code></pre> <p>Where as with the membership they only have </p> <p>public static MembershipProvider Provider { get; private set; }</p> <p>I am not sure though what to change the stuff too. Like what would I change this line too?</p> <p>FormsAuth = formsAuth ?? new FormsAuthenticationWrapper();</p> <p>I also tried to add another method into the FormsAuthentication Interface and Wrapper.</p> <p>public void RedirectFromLoginPage(string userName, bool createPersistentCookie) { FormsAuthentication.RedirectFromLoginPage(userName, createPersistentCookie); }</p> <p>Yet I am not sure what is happening but my unit test always fails does not matter what I try to do to fix it.</p> <pre><code> public ActionResult Login(string returnUrl, FormCollection form, bool rememberMe) { LoginValidation loginValidation = new LoginValidation(); try { UpdateModel(loginValidation, form.ToValueProvider()); } catch { return View("Login"); } if (ModelState.IsValid == true) { bool valid = authenticate.VerifyUser(loginValidation.UserName, loginValidation.Password); if (valid == false) { ModelState.AddModelError("frm_Login", "Either the Password or UserName is invalid"); } else if (string.IsNullOrEmpty(returnUrl) == false) { /* if the user has been sent away from a page that requires them to login and they do * login then redirect them back to this area*/ return Redirect(returnUrl); } else { FormsAuth.RedirectFromLoginPage(loginValidation.UserName, rememberMe); } } return View("Login"); Here is my test </code></pre> <p>[Test] public void Test_If_User_Is_Redirected_Back_To_Page_They_Came_From_After_Login() { System.Diagnostics.Debugger.Break();</p> <pre><code> var formsAuthenticationMock = new Mock&lt;AuthenticationController.IFormsAuthentication&gt;(); var membershipMock = new Mock&lt;MembershipProvider&gt;(); membershipMock.Setup(m =&gt; m.ValidateUser("chobo2", "1234567")).Returns(true); // Setup controller AuthenticationController target = new AuthenticationController(formsAuthenticationMock.Object, membershipMock.Object); // Execute FormCollection form = new FormCollection(); form.Add("Username", "chobo2"); form.Add("password", "1234567"); ViewResult actual = target.Login(null, form, false) as ViewResult; Assert.That(actual.View, Is.EqualTo("home")); formsAuthenticationMock.Verify(); } </code></pre> <p>Actual always comes back to null. I tried ViewResult, RedirectResult and RedirectToRouteResult but everyone comes back null. So I am not sure why this is happening since I find it weird first that </p> <pre><code> FormsAuth.RedirectFromLoginPage(loginValidation.UserName, rememberMe); </code></pre> <p>Does not stop the view and starts to redirect. I thought at first once it hits this line it is like a return statement and thats it no other code will be executed but htis does not seem to be the case so I am not sure if this could be the problem.</p> <p>Thanks</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload