の外には、ここに私のコードです:ここではのHttpContext部品番号私はテストケース</p> <p>の.NETコア1.0.0でのHttpContextをMOQしたいAsp.Netコアのコントローラ
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テキストはnullであってはなりません。ここで
私のサービスである - startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<PromactOauthDbContext>()
.AddDefaultTokenProviders();
services.AddMvc().AddMvcOptions(x => x.Filters.Add(new GlobalExceptionFilter(_loggerFactory)));
}
私がこれに気付かなかったのでしょう。この
のようなテストケースプロジェクトに
登録のHttpContextリポジトリ内のHttpContextを使用まず、あなたのファクトテストメソッドと同じクラスの実際のログインメソッドですか?コントローラを作成せずにログインを直接呼び出すテストはどのように行われますか?コントローラを作成していない場合は、モックを渡すことはできません。 –