title: HttpContext Moq outside of controller in Asp.Net Core
body: <p>I want to moq HttpContext in .net core 1.0.0 for test case</p>
  
  <p>Here is my code:</p>
  
  <pre><code>public async Task&lt;string&gt; Login(string email, string password)
  {
  var result = await _signInManager.PasswordSignInAsync(email, password, false, lockoutOnFailure: false);
  if (result.Succeeded)
     {
         return HttpContext.User.Identity.Name;
     }
     else
     {
         return "";
     }
  }
  </code></pre>
  
  <p>Here is my test case</p>
  
  <pre><code>[Fact]
  public async Task Login()
  {
       ApplicationUser user = new ApplicationUser() { UserName = "siddhartha@promactinfo.com", Email = "siddhartha@promactinfo.com", Name = "siddhartha" };
       await _userManager.CreateAsync(user, "Something@123");
       var userAdded = await _userManager.CreateAsync(user);
       var result = await Login("siddhartha@promactinfo.com", "Something@123");
       Assert.Equal("siddhartha", result);
  }
  </code></pre>
  
  <p>It goes fail, gets error message:</p>
  
  <blockquote>
    <p>HttpContext must not be null.</p>
  </blockquote>
  
  <p>Here is my service - startup.cs</p>
  
  <pre><code> public void ConfigureServices(IServiceCollection services)
      {
          services.AddIdentity&lt;ApplicationUser, IdentityRole&gt;()
              .AddEntityFrameworkStores&lt;PromactOauthDbContext&gt;()
              .AddDefaultTokenProviders();
          services.AddMvc().AddMvcOptions(x =&gt; x.Filters.Add(new GlobalExceptionFilter(_loggerFactory)));
      }
  </code></pre>