2

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 == "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"
        }
    }
SilentRage47
  • 934
  • 2
  • 14
  • 31

1 Answers1

3
  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?

Community
  • 1
  • 1
kaspertorp
  • 188
  • 10