How do I unit test persistence?

后端 未结 10 1279
长发绾君心
长发绾君心 2020-12-23 16:29

As a novice in practicing test-driven development, I often end up in a quandary as to how to unit test persistence to a database.

I know that technically this would

10条回答
  •  春和景丽
    2020-12-23 16:48

    Technically unit tests of persistance are not unit tests. They are integration tests.

    With C# using mbUnit, you simply use the SqlRestoreInfo and RollBack attributes:

        [TestFixture]
        [SqlRestoreInfo(, ,]
        public class Tests
        {
          
            [SetUp]
            public void Setup()
            {
            
            }
    
            [Test]
            [RollBack]
            public void TEST()
            {
               //test insert. 
            }
        }
    

    The same can be done in NUnit, except the attribute names differ slightly.

    As for checking, if your query iss successful, you normally need to follow it with a second query to see if the database has been changed as you expected.

提交回复
热议问题