I am writing a set of integration tests (unit tests with MS Test, which verify that Entity Framework 4.2 stores all classes correctly in the database).
When I run all the tests one by one, they all work fine. When I run them in a group - some of them fail when the wrong number of objects is returned - it seems that db is cleared once at the beginning of the tests, and not between each test - although I see a new context is created and then deleted for each test
Any ideas?
public class EmptyDataInitializer : DropCreateDatabaseAlways<myContext>
{
protected override void Seed(myContext db)
{
db.SaveChanges();
base.Seed(db);
}
}
A Cut device / integration test version
[TestClass]
public class PersistanceTests
{
private myContext _db;
private myContext _dbResults;
private readonly ISettings _configSettings;
public PersistanceTests()
{
_configSettings = MockRepository.GenerateStub<ISettings>();
_configSettings.ConnectionString = "data source=.;initial catalog=myContext_Test; Integrated Security=SSPI; Pooling=false";
Database.SetInitializer(new EmptyDataInitializer());
}
[TestCleanup]
public void TearDownTest()
{
_db.Dispose();
_db = null;
_dbResults.Dispose();
_dbResults = null;
}
[TestInitialize]
public void SetupTest()
{
_db = new myContext(_configSettings);
_dbResults = new myContext(_configSettings);
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()
{
var apple = new Organisation { Name = "Apple" };
_db.Organisations.Add(apple);
_db.SaveChanges();
var organisationsCount = _dbResults.Organisations.Count();
var organisationsAppleCount = _dbResults.Organisations.Where(a => a.Id == apple.Id).Count();
var result = _dbResults.Organisations.FirstOrDefault(a => a.Id == apple.Id);
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");
}
[TestMethod]
public void OrganisationWithNumberOfPeople_PersistanceTest()
{
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);
_db.SaveChanges();
var organisationsCount = _dbResults.Organisations.Count();
var organisationsAppleCount = _dbResults.Organisations.Where(a => a.Id == apple.Id).Count();
var result = _dbResults.Organisations.FirstOrDefault(a => a.Id == apple.Id);
var peopleCountInOrganisation = result.People.Count();
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");
}
}
While performing the tests, I see calls to the SetupTest and TearDownTest methods, but I do not seem to clear the database between the tests.