ASP.NET Core 1.0 AccountControllerに対する単体テストについての記事がいくつもあります。AccountControllerの実際のASP.NET Core 1.0 Identityテーブルに対してxUnitテストを行う方法
本当に実際のAsp.Net Core 1.0 Identityテーブルの内容をテストすることは実際にはないようです。
7つのテーブルがあります。.NETのコアのWeb APIアプリケーションで
AspNetUsers
AspNetUserTokens
AspNetUserLogins
AspNetUserRoles
AspNetUserClaims
AspNetRoles
AspNetRoleClaims
は、(我々が表示されていない外部コードのフレームワーク部分を除く)の流れがメイン
public static void Main(string[] args)
{
var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.Build();
host.Run();
}
を介して第1でありますvar host = ...
から、Startup.csにpublic Startup(IHostingEnvironment env)
と入力します。ランタイム方法public void ConfigureServices(IServiceCollection services)
設定を可能にすることをした後:
// Add framework services.
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
services.AddMvc();
エトセトラ
は最後にhost.Run();
はリスニングモードに私たちの.NETのコアのWeb APIアプリケーションを置きます。
http://localhost:58796/account/login
が原因でログインフォームが返されます。
AccountControllerのコンストラクタは次のようになります。
public AccountController(
UserManager<ApplicationUser> userManager,
SignInManager<ApplicationUser> signInManager,
IEmailSender emailSender,
ISmsSender smsSender,
ILoggerFactory loggerFactory)
{
_userManager = userManager;
_signInManager = signInManager;
_emailSender = emailSender;
_smsSender = smsSender;
_logger = loggerFactory.CreateLogger<AccountController>();
}
これはのUserManagerとSignInManagerはAccountController
に渡さなければならないことを意味します。 SignInManagerはnullにし、null以外のUserManagerと非ヌルロガーを必要とすることはできません。ロガーの設定
public SignInManager(UserManager<TUser> userManager,
IHttpContextAccessor contextAccessor,
IUserClaimsPrincipalFactory<TUser> claimsFactory,
IOptions<IdentityOptions> optionsAccessor,
ILogger<SignInManager<TUser>> logger);
は、私たちのxUnit.net方法が比較的簡単である:
ILoggerFactory loggerFactory = new LoggerFactory()
.AddConsole()
.AddDebug();
public UserManager(IUserStore<TUser> store,
IOptions<IdentityOptions> optionsAccessor,
IPasswordHasher<TUser> passwordHasher,
IEnumerable<IUserValidator<TUser>> userValidators,
IEnumerable<IPasswordValidator<TUser>> passwordValidators,
ILookupNormalizer keyNormalizer,
IdentityErrorDescriber errors,
IServiceProvider services,
ILogger<UserManager<TUser>> logger);
パートの:のUserManagerを設定すると、私たちのxUnit.net方法では、より複雑です
// Summary: Provides an abstraction for a store which manages user accounts.
// TUser: The type encapsulating a user.
public interface IUserStore<TUser> : IDisposable where TUser : class
{
Task<IdentityResult> CreateAsync(TUser user, CancellationToken cancellationToken);
Task<IdentityResult> DeleteAsync(TUser user, CancellationToken cancellationToken);
Task<TUser> FindByIdAsync(string userId, CancellationToken cancellationToken);
Task<TUser> FindByNameAsync(string normalizedUserName, CancellationToken cancellationToken);
Task<string> GetNormalizedUserNameAsync(TUser user, CancellationToken cancellationToken);
Task<string> GetUserIdAsync(TUser user, CancellationToken cancellationToken);
Task<string> GetUserNameAsync(TUser user, CancellationToken cancellationToken);
Task SetNormalizedUserNameAsync(TUser user, string normalizedName, CancellationToken cancellationToken);
Task SetUserNameAsync(TUser user, string userName, CancellationToken cancellationToken);
Task<IdentityResult> UpdateAsync(TUser user, CancellationToken cancellationToken);
}
複雑さのあまりAccountController
の通常の使用のためにASP.NETコア1.0のWeb APIによって、舞台裏で埋葬されています。課題は、店舗ためinterface IUserStore<TUser>
契約を実装の複雑さがありますEF Core 1.0経由でIDデータベーステーブルへのアクセスを提供する配線。
xUnitを使用したユニットテストの場合。モックなしのネット問題は流れは次のようであるということです、(私たちは「実際のデータベース」にアクセスしたいので):問題はキャッチ22で、一言で言えば
[Fact]
// Arrange
...
// Act
... // the AccountController is not available yet
var accountController
= new AccountController(userManager: ...,
signInManager: ...,
emailSender: null,
smsSender: null,
loggerFactory: loggerFactory); // can NOT be null
.............
~~ Start.csは必要なものすべてをセットアップしますが、xUnit.netテストはStart.csが実行される前に実行されています。
.zipファイルはzip形式のないバージョンはたぶんMicrosoft.AspNetCore.TestHost
(統合テストのためのAsp.Netコアに付属に)行くを与えるここ github.com/gerryLowry/EF_Core_testing_experiments
の「本当のデータベース」
UserStore
でUserManager
を構築し、Entity Frameworkの実装では、「進行中の作業」xUnit.netテスト[ファクト] GerryLowryAtSomewhereComCanLogInは()のみ*渡す*表示されます不完全なテスト。 – gerryLowryなぜこれをユニットテストでテストしたいですか?あなた自身のコードをテストしていない。アプリケーション/コードを使用して統合テストを作成したいと思うようなサウンドです。だからそれに合ったフレームワークを選んでください。 – Jocke