I am trying to get integration testing working for my ASP.NET 5 MVC6 Api using EF7. I am using the default project that comes with Identity already installed.
Here is the action I am trying to check in my controller (gets all the children for the logged in user)
[Authorize] [HttpGet("api/children")] public JsonResult GetAllChildren() { var children = _repository.GetAllChildren(User.GetUserId()); var childrenViewModel = Mapper.Map<List<ChildViewModel>>(children); return Json(childrenViewModel); }
In my test project, I create an inmemory database and then run integration tests with this
Here is the base I use for integration tests
public class IntegrationTestBase { public TestServer TestServer; public IntegrationTestBase() { TestServer = new TestServer(TestServer.CreateBuilder().UseStartup<TestStartup>()); } }
And here is TestStartup (where I redefine the method that SQLServer adds with the one that adds the test inmemory database)
public class TestStartup : Startup { public TestStartup(IHostingEnvironment env) : base(env) { } public override void AddSqlServer(IServiceCollection services) { services.AddEntityFramework() .AddInMemoryDatabase() .AddDbContext<ApplicationDbContext>(options => { options.UseInMemoryDatabase(); }); } }
And a test for action
public class ChildTests : IntegrationTestBase { [Fact] public async Task GetAllChildren_Test() {
Can someone point me in the right direction how to potentially set CurrentPrincipal or some other way to make my integration tests work?
Stemo source share