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

前端 未结 3 1966
执笔经年
执笔经年 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:23

    If you are using NUnit, you could use Setup/Teardown attribute with TransactionScope to not commit your changest to the database:

    [SetUp]
    public void SetUp()
    {
        transaction = new TransactionScope();
    }
    
    [TearDown]
    public void TearDown()
    {
        if(transaction != null) 
           transaction.Dispose();
    }
    

    If you are using some other unit test framework it should have simmilar attributes. I would recomend creating a base class DbItegrationTest for all your integration test fixtures so if you derive from this class all test methods won't do commits to database.

    To configure Entity Framework for other database please override db connection string in your test assembly.

提交回复
热议问题