6

Ich versuche Integrationstests für meine ASP.NET 5 MVC6 API mit EF7 zu bekommen. Ich verwende das Standardprojekt, das mit Identity bereits implementiert ist.Integrationstests ASP.NET 5 Identity

Hier ist die Aktion im Versuch, in meinem Controller zu testen (wird alle Kinder für die angemeldeten Benutzer)

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

In meinem Testprojekt erstelle ich eine inmemory Datenbank und führen Sie dann die Integrationstests gegen die hier

ist die Base I für die Integration verwenden Tests

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

Und hier ist die TestStartup (wobei i die Methode überschreiben, die SQLServer fügt mit einem, der die inmemory Testdatenbank)

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

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

} 

und der Test für die Aktion

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 
    } 
} 
fügt

mir jemand Kann in die richtige Richtung weisen, wie die Current oder eine andere Art und Weise, um potenziell gesetzt zu bekommen meine Integrationstests funktionieren?

+0

Did Sie lösen Ihr Problem? –

Antwort

0

Die Frage ist, wie authentifizieren Sie sich in Ihrem Test? Auf dem Projektstart können Sie eine virtuelle Funktion wie hinzufügen unter

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 
    }); 
} 

Und dann eine Startklasse in Ihrem Testprojekt abgeleitet und die Authentifizierungsmethode außer Kraft setzen, nichts zu tun oder Anspruch hinzufügen Sie als unten eine Middleware nutzen können

TestStartUp

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

Mittel ware Klasse

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") 
     }; 
    } 
} 
Verwandte Themen