Nunit数据库回滚

时间:2016-06-17 15:43:28

标签: database nunit integration-testing

我使用Nunit作为测试框架相当新,并且遇到了一些我不太了解的事情。

我正在编写一个集成测试,它将一行插入数据库表中。由于我想反复运行此测试,我想在测试完成后删除该行。测试运行正常,但是当我查看数据库表时,即使执行了删除命令,有问题的行仍然存在。我甚至可以在运行测试时分析数据库时看到它运行。

Nunit会以某种方式回滚数据库事务吗?如果有人有任何想法,为什么我看到这种情况请告诉我。我一直无法找到有关Nunit回滚交易的任何信息,这让我觉得我做错了什么,但测试运行的行出现在数据库中,之后即使探查器显示命令正在被删除运行!

这是我正在运行的测试代码

    [Test]
    [Category("Consultant split integration test")]
    public void Should_Save_Splits()
    {
        var splitList = new List<Split>();

        splitList.Add(new Split()
        {
            UnitGroupId = 69,
            ConsultantUserId = 1,
            CreatedByUserId = 1,
            CreatedOn = DateTime.Now,
            UpdatedByUserId = 1,
            UpdatedOn = DateTime.Now,
            Name = "Consultant1",
            Unit = "Unit1",
            Percentage = 100,
            PlacementId = 47
        });

        var connection = Helpers.GetCpeDevDatabaseConnection();
        var repository = new Placements.ConsultantSplit.DAL.PronetRepository();
        var notebookManager = Helpers.GetNotebookManager(connection);
        var userManager = Helpers.GetUserManager(connection);
        var placementManager = Helpers.GetPlacementManager(connection);
        var sut = new Placements.ConsultantSplit.ConsultantSplitManager(repository, connection, notebookManager, userManager, placementManager);

        IEnumerable<string> errors;
        sut.SaveSplits(splitList, out errors);

        try
        {
            using (connection.Connection)
            {
                using (
                    var cmd = new SqlCommand("Delete from ConsultantSplit where placementid=47",
                        connection.Connection))
                {
                    connection.Open();
                    cmd.CommandType = CommandType.Text;
                    connection.UseTransaction = false;
                    cmd.Transaction = connection.Transaction;
                    cmd.ExecuteNonQuery();
                    connection.Close();
                }
            }
        }
        catch (Exception exp)
        {
            throw new Exception(exp.Message);
        }

0 个答案:

没有答案