Note that there are some explanatory texts on larger screens.

plurals
  1. POEntity Framework Integration tests DropCreateDatabaseAlways not clearing database between test
    primarykey
    data
    text
    <p>I am writing a set of integration tests (Unit tests with MS Test which test that Entity Framework 4.2 is persisting all classes correctly to the database).</p> <p>When I run all tests one by one they all work fine. When I run them in a group - some of them fail as the wrong number of objects are returned - it would seem that the db is being cleaned down once at the start of the tests and not in between each test - even though I can see a new context being created and then disposed of for each test</p> <p>Any Ideas?</p> <pre><code>public class EmptyDataInitializer : DropCreateDatabaseAlways&lt;myContext&gt; { protected override void Seed(myContext db) { //Do Nothing Create Empty Database db.SaveChanges(); base.Seed(db); } } </code></pre> <p>A Cut down version of the unit/integration Tests</p> <pre><code>[TestClass] public class PersistanceTests { //Creating two instances of our Repository so that we can make sure that we are reading from our database rather than in-memory private myContext _db; private myContext _dbResults; private readonly ISettings _configSettings; public PersistanceTests() { _configSettings = MockRepository.GenerateStub&lt;ISettings&gt;(); _configSettings.ConnectionString = "data source=.;initial catalog=myContext_Test; Integrated Security=SSPI; Pooling=false"; Database.SetInitializer(new EmptyDataInitializer()); } //This is called a single time after the last test has finished executing [TestCleanup] public void TearDownTest() { _db.Dispose(); _db = null; _dbResults.Dispose(); _dbResults = null; } //This is called each time prior to a test being run [TestInitialize] public void SetupTest() { _db = new myContext(_configSettings); _dbResults = new myContext(_configSettings); // This forces the database to initialise at this point with the initialization data / Empty DB var count = _db.Accounts.Count(); var resultCount = _dbResults.Accounts.Count(); if (count != resultCount) throw new InvalidOperationException("We do not have a consistant DB experiance."); } [TestMethod] public void OrganisationPersistanceTest() { // Arrange var apple = new Organisation { Name = "Apple" }; _db.Organisations.Add(apple); // Act _db.SaveChanges(); var organisationsCount = _dbResults.Organisations.Count(); var organisationsAppleCount = _dbResults.Organisations.Where(a =&gt; a.Id == apple.Id).Count(); var result = _dbResults.Organisations.FirstOrDefault(a =&gt; a.Id == apple.Id); // Assert Assert.IsTrue(organisationsCount == 1, string.Format("Organisations Count Mismatch - Actual={0}, Expected={1}", organisationsCount, 1)); Assert.IsTrue(organisationsAppleCount == 1, string.Format("Apple Organisations Count Mismatch - Actual={0}, Expected={1}", organisationsAppleCount, 1)); Assert.IsNotNull(result, "Organisations Result should not be null"); Assert.AreEqual(result.Name, apple.Name, "Name Mismatch"); } //A Unit test [TestMethod] public void OrganisationWithNumberOfPeople_PersistanceTest() { // Arrange var person = new Person { Firstname = "Bea" }; var anotherPerson = new Person { Firstname = "Tapiwa" }; var apple = new Organisation { Name = "Apple" }; apple.AddPerson(person); apple.AddPerson(anotherPerson); _db.Organisations.Add(apple); // Act _db.SaveChanges(); var organisationsCount = _dbResults.Organisations.Count(); var organisationsAppleCount = _dbResults.Organisations.Where(a =&gt; a.Id == apple.Id).Count(); var result = _dbResults.Organisations.FirstOrDefault(a =&gt; a.Id == apple.Id); var peopleCountInOrganisation = result.People.Count(); // Assert Assert.IsTrue(organisationsCount == 1, string.Format("Organisations Count Mismatch - Actual={0}, Expected={1}", organisationsCount, 1)); Assert.IsTrue(organisationsAppleCount == 1, string.Format("Apple Organisations Count Mismatch - Actual={0}, Expected={1}", organisationsAppleCount, 1)); Assert.IsNotNull(result, "Organisations Result should not be null"); Assert.AreEqual(result.People.Count, peopleCountInOrganisation, "People count mismatch in organisation Apple - Actual={0}, Expected={1}", peopleCountInOrganisation, 2); Assert.AreEqual(result.Name, apple.Name, "Name Mismatch"); } </code></pre> <p>}</p> <p>Stepping through the tests I can see the SetupTest and TearDownTest methods being called but I it does not seem to clean down the database between tests. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload