Unit Test C# best practice

Asked

Viewed 59 times

0

I have a test code for a method until quite simple to check.

public bool EmPeriodoDeExigencia(ParametroProrrogacao parametroProrrogacao, Requerimento requerimento)
    {
        PreCondicao
            .Para("Determinar o periodo de exigência do requerimento com ou sem prorrogação")
            .EhSatisfeitaCom("Requerimento deve ser diferente de nulo", requerimento != null);


        if (parametroProrrogacao != null && String.IsNullOrEmpty(parametroProrrogacao.Valor))
            return DateTime.Now <= requerimento.DataLimiteParaCumprimentoExigencia.Value.AddDays(Convert.ToInt32(parametroProrrogacao.Valor));
        else
            return DateTime.Now <= requerimento.DataLimiteParaCumprimentoExigencia.Value;

    }

In this code my first concerns is to ensure the basic functioning of the happy path and then make the test of logic and possible holes in it.

Most of the time I understand that for logic is very simple I use the standard AAA, Arrange, Act, Assert, and I can visualize this very well.

But I am stuck on how to use this pattern in these most trivial test cases, would this AAA be the best way to deal with this type of more basic test as well?

An example of one of the codes that is simply valid if a certain parameter is going null:

    [TestMethod]
    [ExpectedException(typeof(ExcecaoDeCondicao))]
    public void EmPeriodoDeExigencia_ParametroERequerimentoNull()
    {
        servico.EmPeriodoDeExigencia(null, null);
    }

In this case I can’t see how I would use an AAA standard, and simply my test is to pass null and perform in the service.

And the second case would be this:

    [TestMethod]
    public void EmPeriodoDeExigencia_ParametrosNotNull_ValorNull()
    {
        //Arrange
        var parametro = new Parametro()
        {
            Chave = "DiasProrrogacaoExigenciaAnaliseDocumental",
            Valor = null
        };
        var requerimentoExigencia = new Requerimento(TipoSolicitacao.Registro, 1);
        requerimentoRepaExigencia.RegistrarExigenciaAnaliseDocumental(requerimentoExigencia, 10);

        //act
        servico.EmPeriodoDeExigencia(parametro, requerimentoExigencia);
    }

Why would I pass, or how would an Assert pass in this situation? This is a question much more than conceptual than to solve a current problem, since the tests are covering and playing their role in the code, but if there is I would like to know a better way to do this since I am beginner in this part of unit tests.

  • Do you use C# 8? If you don’t use it, any reason? If you do, wouldn’t it be better if the parameter no longer accepted null in the compilation?

  • No use, the reason is the project that I am currently inserted in the work is legacy, so this c# ai if I am not mistaken for this project is c# 6. Dotnet Framework.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.