1

Here is my Asp.net Controller Property and Method

    public ApplicationSignInManager SignInManager
    {
        get
        {
            return _signInManager ?? HttpContext.GetOwinContext().Get<ApplicationSignInManager>();
        }
        private set { _signInManager = value; }
    }


    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
    {
        if (!ModelState.IsValid)
        {
            return View(model);
        }

        // This doesn't count login failures towards account lockout
        // To enable password failures to trigger account lockout, change to shouldLockout: true
        var result = await SignInManager.PasswordSignInAsync(model.UserName, model.Password, model.RememberMe, shouldLockout: false);


        switch (result)
        {
            case SignInStatus.Success:
                return RedirectToLocal(returnUrl);             

            default:
                ModelState.AddModelError("", "Invalid login attempt.");
                return View(model);
        }
    }

My Unit Test

 [TestMethod]
    public void Login_WhenEnteredValidCredential_AllowsUserToLogIn()
    {

        var model = new LoginViewModel
        {
            Password = "TestPassWord1",
            UserName = "TestUserName",
            RememberMe = true
        };

        HttpContext.Current = new HttpContext(
    new HttpRequest(null, "http://tempuri.org", null),
    new HttpResponse(null));

       var accountController = new AccountController(); 
        var result= accountController.Login(model,null);
        Assert.AreEqual(result.Status,TaskStatus.RanToCompletion);


    }

The problem here is that whenever i run the unit test HttpContext inside the property SignInManager becomes null. Is there a way to set the HttpContext from the Unit Test? PLease note i have referred to this links but the solution there doesnt work me

Mock HttpContext using moq for unit test

Using httpcontext in unit test

http://caioproiete.net/en/fake-mock-httpcontext-without-any-special-mocking-framework/

Community
  • 1
  • 1
Sike12
  • 1,232
  • 6
  • 28
  • 53
  • 1
    Side note: your unit test is wrong as it is not `await`ing result. Make sure to do so and replace `void` with `async Task` as return type. – Alexei Levenkov May 28 '15 at 15:10

1 Answers1

2

HttpContext.Current which you are faking in your test and accountController.HttpContext do not point to the same object. The answers you reference use HttpContext.Current in the controller code as well.

A better (more testable) approach might be to use ControllerContext.

Community
  • 1
  • 1
prgmtc
  • 765
  • 3
  • 11
  • 1
    `ControllerContext` is indeed the right approach, using `HttpContext.Current` is generally bad idea (especially for `async` code). I think answer you've found actually covers OP's question - so upvote and close as dup. – Alexei Levenkov May 28 '15 at 15:19
  • Thank you for the advice prgmtc and alexei levenkov – Sike12 May 28 '15 at 15:26