mercredi 9 décembre 2015

Testing ASP.NET 5 with Entity Framework 7 using in memory database

I am wanting to get ahold of the Context that I am injecting into the controllers during testing and modify the data in the "in memory" version of the database context.

So the controller looks like this

[Route("api/[controller]")]
public class TestController : Controller
{
    private readonly TestContext _testContext;
    public TestController(TestContext testContext)
    {
        _testContext = testContext;
    }

    [HttpGet]
    public IActionResult Get()
    {
        return Ok(new { _testContext.Users });
    }
}

The test looks like this

public class SiteTests
{
    [Fact]
    public async Task GetIt()
    {
        var server = TestServer.Create(app => { app.UseMvc(); }, services =>
        {
            services.AddMvc();

            services.AddEntityFramework()
            .AddSqlServer()
            .AddDbContext<TestContext>(options => options.UseInMemoryDatabase());

            services.AddScoped<TestContext, TestContext>();
        });

        var client = server.CreateClient();

        var response = await client.GetAsync("http://localhost/api/test");
        var content = await response.Content.ReadAsStringAsync();

        Assert.True(response.IsSuccessStatusCode);
    }
}

I would love to somehow get ahold of the context before the client gets the request and modify what data will be coming back from the database context.

I have the test project in GitHub

http://ift.tt/1lQbp4u

Any help would be much appreciated.

Aucun commentaire:

Enregistrer un commentaire