lundi 8 juin 2020

Prevent a method to be executed during Unit testing

I am trying to test a class called "PuntuadorJuego"(GameScore) which has a method “PuntuarXAcertar”(PointsIfMatching) that call a private method “Actualiza”(Update) that basically Updates the score on the Unity's interface as you can see in the code bellow, the problem is that every time I run the test it stops there. I have tried commented the line that call the method and it works but I wonder if there is any other way to prevent the method "Actualiza" to be called during testing or even better if there is a way to ignore code related to the interface while testing. Thanks.

[Testing class]
 public class PuntuadorTest
    {
        [Test]
        public void TestPuntuacionAcertar()
        {
            //Assign
            PuntuadorJuego puntuador = new PuntuadorJuego(puntuacion: 50);

            //Act
            puntuador.PuntuarXAcertar(esTurnoJ1: true);

            //Assert
            Assert.AreEqual(expected: 60, actual: puntuador.GetPuntuacionJ1());
        }
    }


[Method called by the tested Method]
private void Actualiza(int cantidad, bool esTurnoJ1)
    {
        if (esTurnoJ1)
        {
            if (puntuacionJ1 < 0)
            {
                ValorPuntuacionText.color = Color.red;
            }
            else
            {
                ValorPuntuacionText.color = Color.white; //THIS is the error line
            }
            ValorPuntuacionText.text = puntuacionJ1 + "";
        }
        else
        {
            if (puntuacionJ2 < 0)
            {
                ValorPuntuacionJ2Text.color = Color.red;
            }
            else
            {
                ValorPuntuacionJ2Text.color = Color.white;
            }
            ValorPuntuacionJ2Text.text = puntuacionJ2 + "";
        }

        if (cantidad < 0)
        {
            burbujaPuntuacion.color = Color.red;
        }
        else 
        {
            burbujaPuntuacion.color = Color.green;
        }
        burbujaPuntuacion.text = cantidad + "";
        burbujaAnimacion.Play("Puntua");


    }


[Tested Method]
    public void PuntuarXAcertar(bool esTurnoJ1 = true) 
    {
        if (esTurnoJ1)
        {
            puntuacionJ1 += ACERTAR;
        }
        else
        {
            puntuacionJ2 += ACERTAR;
        }
        Actualiza(ACERTAR, esTurnoJ1);
    }

PS: I am using C#, Visual Studio and Unity.

Aucun commentaire:

Enregistrer un commentaire