SilentRage47
SilentRage47

Reputation: 952

MySQL Start transaction/rollback doesn't work in integration test

I'm trying to create an integration test for my Userclass using NUnit, what I want do is start a transaction, update my MySQL test db and than rollback.

This is my code, the update is fine but at the and my db remains updated. Calling my query in MySQL Workbench works fine.

[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 == "[email protected]")
            {
                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 = "[email protected]",
                attivo = 0
            };
            user.Update();
            user = new User(1);

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

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

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

Upvotes: 2

Views: 434

Answers (1)

kaspertorp
kaspertorp

Reputation: 198

  1. check if your database supports transactions.

  2. You can use the Transactionscope to test transactions. see. How do I test database-related code with NUnit?

Upvotes: 3

Related Questions