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

オープン
#294 コメント 5 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
3/5
見積もり時間
1〜2日
初心者へのやさしさ
35/100
issue の種類
ドキュメント
明瞭さ
おおむね明確
活発さ
停滞
技術スタック
csharp

調査の方向性

まずディスカッション #168 と、この issue にあるドキュメント改善の提案を読み、その後、関連する NUnit の assertion ドキュメントを見つけます。Assert の動作変更を文書化し、実装の詳細に依存することが推奨されない理由を説明し、Assert.That と Assert.Throws の代替手段を示せば完了です。

索引モデルが issue の本文から書いたものです。

説明

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.

主要言語
Dockerfile
スター
601
フォーク
159
平均マージ
22時間 18分
マージ済み PR(30日)
6

環境構築

Codespaces で開く

このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

nunit/docs のほかの issue

nunit/docs の issue をすべて見る

似ている issue

Documentation の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。