How should I set up my integration tests to use a test database with Entity Framework?

前端 未结 3 2001
执笔经年
执笔经年 2020-12-02 08:44

I am writing integration tests for an application and have not been able to find any best practices on how to set up a test database for my integration suite. I am working o

3条回答
  •  误落风尘
    2020-12-02 09:09

    Just setup a connection string in the app.config of your unit test project that points to the new DB instance.

    You can then use the initialisation and cleanup methods in your test class to create and delete the DB.

    The connection string is just the usual, e.g.

    
    

    Then to create the DB, once per test, you could do:

        YourContext _ctx;
    
        [TestInitialize]
        public  void Initiaslise()
        {
    
            YourNameDbInitialise initialiser = new YourNameDbInitialiseForTest();
            Database.SetInitializer(initialiser);
    
            _ctx = new YourNameContext();
    
            initialiser.InitializeDatabase(_ctx);         
        }
    

    and this to delete at the end of each test

        [TestCleanup]
        public  void Cleanup()
        {
            Database.Delete("YourName");
        }
    

提交回复
热议问题