Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6919795
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:02:43+00:00 2026-05-27T10:02:43+00:00

I am writing a set of integration tests (Unit tests with MS Test which

  • 0

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).

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

Any Ideas?

public class EmptyDataInitializer : DropCreateDatabaseAlways<myContext>
{
    protected override void Seed(myContext db)
    {
        //Do Nothing Create Empty Database
        db.SaveChanges();
        base.Seed(db);
    }
}

A Cut down version of the unit/integration Tests

[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<ISettings>();
        _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 => a.Id == apple.Id).Count();
        var result = _dbResults.Organisations.FirstOrDefault(a => 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 => a.Id == apple.Id).Count();
        var result = _dbResults.Organisations.FirstOrDefault(a => 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");
   }

}

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T10:02:43+00:00Added an answer on May 27, 2026 at 10:02 am

    Okay even Better answer – add a database.Initialize(force: true);
    into the TestInitialize method.

    [TestInitialize]
    public void SetupTest()
    {          
        _db = new myContext(_configSettings);
        _db.Database.Initialize(force: true);
        _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.");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I plan to introduce a set of standards for writing unit tests into my
I am writing a set of database-driven applications in PHP. These applications will run
I'm writing a set of functions in c++ which can be called by excel.
I find myself writing code that looks like this a lot: set<int> affected_items; while
I'm writing an winforms app that needs to set internet explorer's proxy settings and
When I'm writing my DAL or other code that returns a set of items,
I'm writing a bash script and I have errexit set, so that the script
I'm writing a plugin that will allow parameters to 'set it up.' But I
I am writing a bash script to run an integration test of a tool
I'm writing a set of collection classes for different types of Trees. I'm doing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.