Sto cercando di ottenere il test di integrazione funzionante per il mio ASP.NET 5 MVC6 Api utilizzando EF7. Sto usando il progetto predefinito che viene fornito con Identity già implementato.test di integrazione ASP.NET 5 Identity
Ecco l'im azione cercando di testare a mio controller (ottiene tutti i bambini per l'utente collegato)
[Authorize]
[HttpGet("api/children")]
public JsonResult GetAllChildren()
{
var children = _repository.GetAllChildren(User.GetUserId());
var childrenViewModel = Mapper.Map<List<ChildViewModel>>(children);
return Json(childrenViewModel);
}
Nel mio progetto di test creo una base di dati INT e poi fare il test di integrazione nei confronti di tale
qui è la base che uso per l'integrazione test
public class IntegrationTestBase
{
public TestServer TestServer;
public IntegrationTestBase()
{
TestServer = new TestServer(TestServer.CreateBuilder().UseStartup<TestStartup>());
}
}
Ed ecco la TestStartup (dove ho l'override del metodo che aggiunge SQLServer con uno che aggiunge il database di test INT)
public class TestStartup : Startup
{
public TestStartup(IHostingEnvironment env) : base(env)
{
}
public override void AddSqlServer(IServiceCollection services)
{
services.AddEntityFramework()
.AddInMemoryDatabase()
.AddDbContext<ApplicationDbContext>(options => {
options.UseInMemoryDatabase();
});
}
}
e il test per l'azione
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
}
}
nessuno mi può punto nella giusta direzione su come impostare potenzialmente la CurrentPrincipal o qualche altro modo per ottenere i miei test di integrazione funzionano?
Forse hai risolto il tuo problema? –