6

EF7'yi kullanarak ASP.NET 5 MVC6 Api için tümleştirme sınamalarını denemeye çalışıyorum. Önceden uygulanan Kimlik ile gelen varsayılan projeyi kullanıyorum. İşte tümleştirme sınaması ASP.NET 5 Kimlik

benim denetleyicisi sınıyor eylem im olan Ben BELLEK veritabanı oluşturmak ve sonra buna karşı entegrasyon testleri yapmak benim test projede

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

(oturum açan kullanıcı için tüm çocukları alır) İşte

Ben entegrasyon için kullanmak Taban

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

testleri Ve burada ben SQLServer ekler yöntemini geçersiz nerede (TestStartup olduğunu BELLEK testi veritabanı ekler biri)

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

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

} 

Ve eylem için test ile

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

herkes potansiyel CurrentPrincipal veya almak için başka bir yolu nasıl ayarlanacağı ile ilgili doğru yönde işaret edebilir uyum testleri benim çalışıyor mu?

+0

mü problemini çözdün mü? –

cevap

0

Sorunuz, testinizde nasıl doğrulanıyorsunuz? Projeniz Başlangıçta size

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

Ve sonra test projede bir başlangıç ​​sınıfı türetilmiş aşağıdaki gibi bir sanal işlev eklemek ve hiçbir şey yapmak ya da aşağıdaki gibi bir orta-ware kullanabilirsiniz iddia eklemek için kimlik doğrulama yöntemini geçersiz kılabilir TestStartUp

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

Orta eşya sınıfı

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