How can I create and populate my mock classes with Autofixture?

后端 未结 2 1171
梦如初夏
梦如初夏 2021-02-07 10:58

Currently I\'m using EF6 to implement my repositories inside a UnitOfWork. I also have created an In-Memory mock implementations (MockUnitOfWork & MockRepository) so that I

2条回答
  •  青春惊慌失措
    2021-02-07 11:19

    You're trying to do functional testing here, so it would be wise to have a functional database.

    EF can recreate and destroy your database in your setup and teardown methods with a test connection string. This would provide a real functional testing environment for your tests to operate against mimicking the real environment.

    Ex:

            [TestFixtureSetUp]
            public static void SetupFixture() //create database
            {
                using (var context = new XEntities())
                {
                    context.Setup();
                }
            }
    
            [TestFixtureTearDown]
            public void TearDown() //drop database
            {
                using (var context = new XEntities())
                {
                    context.Database.Delete();
                }
            }
    
            [SetUp]
            public void Setup() //Clear entities before each test so they are independent
            {
                using (var context = new XEntities())
                {
                    foreach (var tableRow in context.Table)
                    {
                        context.Table.Remove(tableRow);
                    }
                    context.SaveChanges();
                }
            }
    

提交回复
热议问题