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/
awaiting result. Make sure to do so and replacevoidwithasync Taskas return type.