控制器Asp.Net之外我要起订量的HttpContext在.NET核心1.0.0测试案例HttpContext的起订量核心
这里是我的代码:
public async Task<string> 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 "";
}
}
这里是我的测试案例
[Fact]
public async Task Login()
{
ApplicationUser user = new ApplicationUser() { UserName = "[email protected]", Email = "[email protected]", Name = "siddhartha" };
await _userManager.CreateAsync(user, "[email protected]");
var userAdded = await _userManager.CreateAsync(user);
var result = await Login("[email protected]", "[email protected]");
Assert.Equal("siddhartha", result);
}
它去失败,得到错误信息:
HttpCon文字不能为空。
这里是我的服务 - startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<PromactOauthDbContext>()
.AddDefaultTokenProviders();
services.AddMvc().AddMvcOptions(x => x.Filters.Add(new GlobalExceptionFilter(_loggerFactory)));
}
我没有注意到这一点,在第一,但是你的事实测试方法和你的实际登录方法在同一个类中?测试如何直接调用Login而不创建Controller?如果你没有创建控制器,你不能通过模拟。 –