none
Database.SetInitializer. Can it only be set once? Having trouble unit testing multiple initializers RRS feed

  • Question

  • I've subclassed DropCreateDatabaseAlways, DropCreateDatabaseIfModelChanges, and CreateDatabaseIfNotExists so that we can automate deployments of our database more smoothly (running scripts for all non-EF generated stuff such as triggers, sp's, , unique constraints, etc and any other additional seeding scripts).  I've created a Unit Test project to test each of these Initializer, but it always fails after the first test.

    The test setup will drop the database and recreate it as follows (using sqlce 4 btw):

    [TestInitialize]
        public void TestInitialize()
        {
          Database.Delete("MyDataContext");
          var engine = new SqlCeEngine(Util.ConnectionString);
          engine.CreateDatabase();
        }
    


    Then each test sets the Initializer it wants to test, as follows:

    [TestMethod]
        public void TestDeployAlwaysNoScripts()
        {
          Database.SetInitializer(new DeployAlwaysInitializer());
          using (var dc = new MyDataContext())
          {
            dc.Sessions.FirstOrDefault();
          }
        }
    

    So there are three tests, testing each custom Initializer, the first one always succeeds and subsequent tests always fail:  Subsequent tests appear not to do any initialization and expect the tables to exist. I imagine there is some sort of static state here which isn't making this very conducive to unit testing.  Is there anyway around this??


    W. Brian
    Monday, June 13, 2011 3:55 PM

Answers

  • Figured it out, I need to pass true to the dataContext.DataBase.Initialize method to for initialization even if it's already occurred.
    W. Brian
    • Marked as answer by WBrianG Tuesday, June 14, 2011 10:57 PM
    Tuesday, June 14, 2011 10:57 PM

All replies