6

Próbuję uzyskać testy integracji działające na moim ASP.NET 5 MVC6 Api przy użyciu EF7. Korzystam z domyślnego projektu, który jest już zaimplementowany.testowanie integracji ASP.NET 5 Tożsamość

Oto im działanie próbuje przetestować w moim kontrolera (dostaje wszystkie dzieci dla zalogowanego użytkownika)

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

W moim projekcie badania utworzyć WEW bazy danych, a następnie wykonaj testy integracyjne przeciwko temu

Oto podstawowa używać do integracji testuje

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

A oto TestStartup (gdzie i zastąpić metodę, która dodaje SQLServer z jednym, który dodaje bazę testową WEW)

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

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

} 

i test działania

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

Czy ktoś może wskazać mi w dobrym kierunku, w jaki sposób potencjalnie ustawić CurrentPrincipal lub jakiś inny sposób, aby uzyskać moje testy integracyjne działają?

+0

Czy rozwiązujesz swój problem? –

Odpowiedz

0

Pytanie brzmi, w jaki sposób uwierzytelniasz się podczas testu? Na swoim starcie projektu można dodać funkcję wirtualnego jak poniżej

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

A potem pochodzi klasę startowego w projekcie testowym i zastąpić metodę uwierzytelniania, aby nic nie robić lub dodawania roszczenia można użyć środkowy-ware jak poniżej

TestStartUp

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

średniej klasy wyroby

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") 
     }; 
    } 
} 
Powiązane problemy