【问题标题】:MySQL Start transaction/rollback doesn't work in integration testMySQL 开始事务/回滚在集成测试中不起作用
【发布时间】:2016-03-17 15:03:36
【问题描述】:

我正在尝试使用NUnit 为我的Userclass 创建一个集成测试,我想做的是启动一个transaction,更新我的MySQL 测试数据库而不是回滚。

这是我的代码,更新很好,但我的数据库仍然更新。 在MySQL Workbench 中调用我的查询可以正常工作。

[TestFixture]
    public class UtenteIntTest
    {
        private User user;
        bool result;

        [SetUp]
        public void Setup()
        {
            //Execute query "START TRANSACTION"
            result = false;
        }

        [Test] 
        public void ConstId_IdUser_User()
        {
            user = new User(1);

            if ((user.id == 1) && user.username == "test" && 
                user.name == "test" && user.active == 1 
                && user.mail == "test@test.test")
            {
                result = true;
            }

            Assert.That(result, Is.EqualTo(true));
        }

        [Test]
        public void Update_User_UpdatedUser()
        {
            user = new User(1)
            {
                username = "update",
                password = "update",
                name = "update",
                mail = "update@update.update",
                attivo = 0
            };
            user.Update();
            user = new User(1);

            if ((user.id == 1) && user.username == "update" &&
                user.nominativo == "update" && user.active == 0
                && user.mail == "update@update.update")
            {
                result = true;
            }

            Assert.That(result, Is.EqualTo(true));
        }

        [TearDown]
        public void Teardown()
        {
                //Execute query "ROLLBACK"
        }
    }

【问题讨论】:

    标签: c# mysql transactions nunit integration-testing


    【解决方案1】:
    1. 检查您的数据库是否支持事务。

    2. 您可以使用事务范围来测试事务。看。 How do I test database-related code with NUnit?

    【讨论】:

    • 我在调用我的 Update 方法之前尝试了 TransactionScope,它工作正常。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-01
    • 2017-07-18
    • 2017-02-18
    • 2016-07-26
    相关资源
    最近更新 更多