9
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

.Net + SQL Serverでテストデータを準備する方法

Last updated at Posted at 2012-02-16

基本(もっともシンプルな実装)

1.テストメソッド内で、TransactionScopeを生成する。
2.テストデータを挿入
3.テストメソッド内で、目的の処理を記述
4.テスト終了前にDispose()し、トランザクションを破棄する
5.データベースは元どおり

TestDataPreparation001.cs
       [TestMethod()]
        public void Test_001()
        {
            using (var trans = new TransactionScope(TransactionScopeOption.Required))
            {
                // inserting test data
                this.InsertData();
                
                // start testing
                var target = new Testee();
                var expected = 0;
                var actual = target.Something;
                Assert.AreEqual(expected, actual);
                
                // revert test data to original
                trans.Dispose();
            }
        }
9
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?