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ą?
Czy rozwiązujesz swój problem? –