How to isolate EF InMemory database per XUnit test
<p>I am trying use InMemory EF7 database for my xunit repository test. </p>

<p>But my problem is that when i try to Dispose the created context the in memory db persist. It means that one test involve other. </p>

<p>I have read this article <a href="https://davidzych.com/unit-testing-entity-framework-7-with-the-in-memory-data-store/" rel="noreferrer">Unit Testing Entity Framework 7 with the In Memory Data Store</a> and I have tried to setup the context in the constructor of my  TestClass. But this approach doesn't work. When I run tests separately everything is OK, but my first test method add something into DB and second test method start with dirty DB from previous test method. I try add <code>IDispose</code> into test class but method DatabaseContext and DB persist in memory. What I am doing wrong am i missing something? </p>

<p>My code looks like:</p>

<pre><code>using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Xunit;

namespace Fabric.Tests.Repositories
{
    /// &lt;summary&gt;
    /// Test for TaskRepository 
    /// &lt;/summary&gt;
    public class TaskRepositoryTests:IDisposable
    {

        private readonly DatabaseContext contextMemory;

        /// &lt;summary&gt;
        /// Constructor
        /// &lt;/summary&gt;
        public TaskRepositoryTests()
        {
            var optionsBuilder = new DbContextOptionsBuilder&lt;DatabaseContext&gt;();
            optionsBuilder.UseInMemoryDatabase();
            contextMemory = new DatabaseContext(optionsBuilder.Options);

        }

        /// &lt;summary&gt;
        /// Dispose DB 
        /// &lt;/summary&gt;
        public void Dispose()
        {
            //this has no effect 
            if (contextMemory != null)
            {                
                contextMemory.Dispose();
            }
        }


        /// &lt;summary&gt;
        /// Positive Test for ListByAssigneeId method  
        /// &lt;/summary&gt;
        /// &lt;returns&gt;&lt;/returns&gt;       
        [Fact]
        public async Task TasksRepositoryListByAssigneeId()
        {
            // Arrange
            var assigneeId = Guid.NewGuid();
            var taskList = new List&lt;TaskItem&gt;();


            //AssigneeId != assigneeId 
            taskList.Add(new TaskItem()
            {
                AssigneeId = Guid.NewGuid(),
                CreatorId = Guid.NewGuid(),
                Description = "Descr 2",
                Done = false,
                Id = Guid.NewGuid(),
                Location = "Some location 2",
                Title = "Some title 2"
            });

            taskList.Add(new TaskItem()
            {
                AssigneeId = assigneeId,
                CreatorId = Guid.NewGuid(),
                Description = "Descr",
                Done = false,
                Id = Guid.NewGuid(),
                Location = "Some location",
                Title = "Some title"
            });

            taskList.Add(new TaskItem()
            {
                AssigneeId = assigneeId,
                CreatorId = Guid.NewGuid(),
                Description = "Descr 2",
                Done = false,
                Id = Guid.NewGuid(),
                Location = "Some location 2",
                Title = "Some title 2"
            });

            //AssigneeId != assigneeId 
            taskList.Add(new TaskItem()
            {
                AssigneeId = Guid.NewGuid(),
                CreatorId = Guid.NewGuid(),
                Description = "Descr 2",
                Done = false,
                Id = Guid.NewGuid(),
                Location = "Some location 2",
                Title = "Some title 2"
            });


            //set up inmemory DB            
            contextMemory.TaskItems.AddRange(taskList);

            //save context
            contextMemory.SaveChanges();

            // Act
            var repository = new TaskRepository(contextMemory);
            var result = await repository.ListByAssigneeIdAsync(assigneeId);

            // Assert
            Assert.NotNull(result.Count());

            foreach (var td in result)
            {
                Assert.Equal(assigneeId, td.AssigneeId);
            }

        }

        /// &lt;summary&gt;
        /// test for Add method  
        /// (Skip = "not able to clear DB context yet")
        /// &lt;/summary&gt;
        /// &lt;returns&gt;&lt;/returns&gt;
        [Fact]
        public async Task TasksRepositoryAdd()
        {
            var item = new TaskData()
            {
                AssigneeId = Guid.NewGuid(),
                CreatorId = Guid.NewGuid(),
                Description = "Descr",
                Done = false,
                Location = "Location",
                Title = "Title"
            };


            // Act
            var repository = new TaskRepository(contextMemory);
            var result = await repository.Add(item);

            // Assert
            Assert.Equal(1, contextMemory.TaskItems.Count());
            Assert.NotNull(result.Id);

            var dbRes = contextMemory.TaskItems.Where(s =&gt; s.Id == result.Id).SingleOrDefault();
            Assert.NotNull(dbRes);
            Assert.Equal(result.Id, dbRes.Id);
        }


    }
}
</code></pre>

<p>I am using:</p>

<pre><code>"Microsoft.EntityFrameworkCore.InMemory": "1.0.0"

"Microsoft.EntityFrameworkCore": "1.0.0"

"xunit": "2.2.0-beta2-build3300"
</code></pre>