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/

1
  • 1
    Side note: your unit test is wrong as it is not awaiting result. Make sure to do so and replace void with async Task as return type. Commented May 28, 2015 at 15:10

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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.
Thank you for the advice prgmtc and alexei levenkov

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.