6

私は、ASP.NET 5 MVC6 ApiでEF7を使用して統合テストを行っています。既に実装されているIdentityに付属のデフォルトプロジェクトを使用しています。ここで 統合テストASP.NET 5 ID

は私のコントローラでテストしようとしているアクションイムである私は、インメモリデータベースを作成し、その反対の統合テストを行う私のテストプロジェクトで

[Authorize]  
[HttpGet("api/children")] 
public JsonResult GetAllChildren() 
{ 
    var children = _repository.GetAllChildren(User.GetUserId()); 
    var childrenViewModel = Mapper.Map<List<ChildViewModel>>(children); 
    return Json(childrenViewModel); 
} 

(ログインしているユーザーのためにすべての子を取得します)ここで

は、私は統合テスト

public class IntegrationTestBase 
{ 
    public TestServer TestServer; 
    public IntegrationTestBase() 
    { 
     TestServer = new TestServer(TestServer.CreateBuilder().UseStartup<TestStartup>()); 
    } 
} 

に使用するベースであり、ここで私はSQLServerのを追加するメソッドをオーバーライドしてTestStartup(ありますインメモリ・テスト・データベースを追加します1)

public class TestStartup : Startup 
{ 
    public TestStartup(IHostingEnvironment env) : base(env) 
    { 
    } 

    public override void AddSqlServer(IServiceCollection services) 
    { 
     services.AddEntityFramework() 
      .AddInMemoryDatabase() 
      .AddDbContext<ApplicationDbContext>(options => { 
       options.UseInMemoryDatabase(); 
      }); 
    } 

} 

と行動のためのテストと

public class ChildTests : IntegrationTestBase 
{ 
    [Fact] 
    public async Task GetAllChildren_Test() 
    { 
     //TODO Set Current Principal?? 

     var result = await TestServer.CreateClient().GetAsync("/api/children"); 
     result.IsSuccessStatusCode.Should().BeTrue(); 

     var body = await result.Content.ReadAsStringAsync(); 
     body.Should().NotBeNull(); 
     //TODO more asserts 
    } 
} 

誰もが潜在的にCurrentPrincipalまたは取得するための他の方法を設定する方法については正しい方向に私を指すことができます私の統合テストは機能していますか?

+0

んでしたあなたはあなたの問題を解決しますか? –

答えて

0

質問はあなたのテストでどのように認証されていますか?プロジェクトの起動時に、あなたは

以下
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) 
{ 
    // ... 
    UseAuthentication(app); 

    // ... 

    app.UseMvcWithDefaultRoute(); 
} 

protected virtual void UseAuthentication(IApplicationBuilder app) 
{ 
    app.UseCookieAuthentication(new CookieAuthenticationOptions 
    { 
     AuthenticationScheme = "Cookies", 
     AutomaticAuthenticate = true, 
     AutomaticChallenge = true 
    }); 
} 

のような仮想関数を追加することができそして、あなたのテストプロジェクトに起動クラスを派生し、何もしないようにしたり、以下のようにミドルウェアを使用することができます主張を追加するための認証メソッドをオーバーライド

てTestStartup

internal TestStartUp : Startup 
{ 
    protected override void UseAuthentication(IApplicationBuilder app) 
    { 
     app.UseMiddleware<TestAuthMiddlewareToByPass>(); 
    } 
} 

ミドルウェアクラス

public class TestAuthMiddlewareToByPass 
{ 
    public const string TestingCookieAuthentication = "TestCookieAuthentication"; 

    private readonly RequestDelegate _next; 

    public TestAuthMiddlewareToByPass(RequestDelegate next) 
    { 
     _next = next; 
    } 

    public async Task Invoke(HttpContext context) 
    { 
     // fake user 
     ClaimsIdentity claimsIdentity = new ClaimsIdentity(Claims(), TestingCookieAuthentication); 

     ClaimsPrincipal claimsPrincipal = new ClaimsPrincipal(claimsIdentity); 

     context.User = claimsPrincipal; 

     await _next(context); 
    } 

    protected virtual List<Claim> Claims() 
    { 
     return new List<Claim> 
     { 
      new Claim(ClaimTypes.Name, "admin"), 
      new Claim(ClaimTypes.Role, "admin") 
     }; 
    } 
}