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

Offen
#294 5 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Bewertung

Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Anfängerfreundlichkeit
35/100
Issue-Typ
Dokumentation
Klarheit
Größtenteils klar
Aktivitätsstatus
Veraltet
Tech-Stack
csharp

Rechercherichtung

Beginne mit dem Lesen der Diskussion #168 und des Vorschlags zur Verbesserung der Dokumentation in diesem Issue. Suche anschließend die relevante NUnit-Dokumentation zu Assertions. Als erledigt gilt die Aufgabe, wenn die Verhaltensänderung von Assert dokumentiert, erklärt wird, warum man sich nicht auf Implementierungsdetails verlassen sollte, und die Alternativen Assert.That und Assert.Throws gezeigt werden.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

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.

Vorherrschende Sprache
Dockerfile
Sterne
601
Forks
159
Ø Merge
22 Std. 18 Min.
Gemergte PRs (30 T.)
6

Entwicklungsumgebung

In Codespaces öffnen

Startet den Dev-Container des Projekts im Browser, mit Ihrem eigenen GitHub-Konto.

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus nunit/docs

Alle Issues in nunit/docs

Ähnliche Issues

Weitere Issues zu Documentation

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.