Improve Documentation To Highlight Reliance on Implementation Details of Assert As Bad

Abierto
#294 5 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
3/5
Tiempo estimado
1-2 días
Aptitud para principiantes
35/100
Tipo de issue
Documentación
Claridad
Bastante claro
Estado de actividad
Estancado
Stack tecnológico
csharp

Línea de trabajo

Comienza leyendo la discusión #168 y la sugerencia de mejora de la documentación en este issue; después, localiza la documentación relevante de NUnit sobre las aserciones. Se considera terminado cuando se documente el cambio de comportamiento de Assert, se explique por qué no se recomienda depender de los detalles de implementación y se muestren las alternativas Assert.That y Assert.Throws.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

area:framework

This is a continuation of this discussion: #168 I completely agree with @CharliePoole on this, but I would suggest that the documentation be improved to highlight the situation.

-- Begin Documentation Improvement Suggestion --

Behavior of Asserts
[Test]
public void TestAssertCatch()
{
    try
    {
        Assert.That(true, Is.False);
    }
    catch
    {
    }
}

When looking at this example you may expect NUnit to Assert that true != false. However in versions of NUnit Prior to 3.6.0.0 this test would pass due to an implementation detail of Assert. In newer versions of NUnit this will Assert (and fail the test) as a design decision of the NUnit.org Team.

In cases where this is used, it was probably used in scenarios where better APIs could be leveraged.

Consider the following test which is trying to Assert that an Exception is thrown/not thrown:

public void TestException()
{
    try
    {
        GuessTheNumber(42);

        // This code only executes if no exception has occurred
        Assert.Fail("An expected exception did not occur");
    }
    catch(Exception ex)
    {
        Assert.That(ex.Message, Is.EqualTo("The Answer"), "An expected exception did not occur");
    }
}

public static void GuessTheNumber(int number)
{
    if(number == 42)
    {
        throw new Exception("The Answer");
    }
}

Instead this should be rewritten to use either Assert.That or Assert.Throws like so:

[Test]
public void TestExceptionAssertThat()
{
    Assert.That(() => GuessTheNumber(42), Throws.InstanceOf<Exception>().With.Property("Message").EqualTo("The Answer"), "An expected exception did not occur");
}

Or Assert.Throws Style

[Test]
public void TestExceptionAssertThrows()
{
    Exception ex = Assert.Throws<Exception>(() => GuessTheNumber(42), "An expected exception did not occur");
    Assert.That(ex.Message, Is.EqualTo("The Answer"), "An expected exception did not occur");
}

-- End Documentation Improvement --

As Charlie mentioned:

I have asked the submitter of the issue to give a motivating example that would help us meet the need

It would be nice to hear what other scenarios end users are encountering that cause them to need to capture the exception of the Assert. The above example is just what we found in our code base during our 2.6.4->3.11.0 Convert.

Thank you for this tool; it is invaluable to us.

Lenguaje dominante
Dockerfile
Estrellas
601
Forks
159
Merge medio
22 h 18 min
PR fusionados (30 d)
6

Preparar el entorno

Abrir en Codespaces

Inicia el contenedor de desarrollo del proyecto en tu navegador, con tu propia cuenta de GitHub.

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de nunit/docs

Todos los issues de nunit/docs

Issues similares

Más issues de Documentation

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.