簡體   English   中英

NUnit測試中Assert.Throws異常的問題

[英]Issue with Assert.Throws Exception in NUnit testing

我是第一次嘗試NUnit,單元測試和集成測試。 我一直在閱讀並做很多在線課程。 我相信您非常了解,這是了解理論並在實踐中進行的工作。

我被困在特定的測試中。 我擁有的應用程序在C#.Net 3.5中。

我試圖斷言具有錯誤輸入的方法將引發特定異常。 當我使用與測試相同的輸入來運行該方法時,將引發預期的異常。

被測試的方法代碼為:

 private static bool FilePathHasInvalidChars(string userInputPath)
{
    try
    {
        Path.GetFullPath(userInputPath);//this is where the warning appears

    }
    catch (Exception e)
    {
        Log.Error(String.Format(
            "The Program failed to run due to invalid characters or empty string value for the Input Directory. Full Path : <{0}>. Error Message : {1}.",
            userInputPath, e.Message), e);
        return true;

    }
    return false;
}

如果提供的輸入目錄不符合條件,我想檢查以上代碼是否可以捕獲異常。

我目前的單元測試是:

    [Test]
    public void can_throws_exception_for_empty_string()
    {
        var testDirectory = "";
        var sut = new DirectoryInfoValidator();

        Assert.Throws<ArgumentNullException>(() => sut.FilePathHasInvalidChars(testDirectory));
    }

我的問題是測試始終失敗,並且如果我檢查返回值,它會指出它期望ArgumentNull異常,但為null。 我已經截取了測試輸出的屏幕截圖: 拋出測試輸出

知道我做錯了什么嗎? 編輯:順便說一句我也嘗試使用

[ExpectedException(typeof(ArgumentNullException), ExceptionMessage= "Log Message", MatchType=MessageMatch.Contains)]

產生了相同的結果。

最后,我不確定這是集成測試還是單元測試,因為我的方法使用Path.GetFullPath(string directory)。 無論如何,我現在的主要問題是了解我在做什么錯。 :)
非常感謝Jetnor。

更新:在考慮了所有要點並考慮了系統的需求之后,我決定不拋出異常。 相反,我決定創建測試以涵蓋在我的情況下可能發生的各種可能的異常。 測試方法如下:

        [Test]
    public void returns_true_for_empty_string()
    {
        var testDirectory = "";
        var sut = new DirectoryInfoValidator();
        var isInvalidPath = sut.FilePathHasInvalidChars(testDirectory);
        Assert.That(isInvalidPath, Is.True);
    }

這是一個起點。 我通過將所有輸入提供給一個測試並同時檢查所有輸入來使用[TestCase]選項。 感謝您的幫助。

您的方法FilePathHasInvalidChars不會引發異常。 方法內部會引發異常,但是您的方法會捕獲並處理該異常。 您的方法將始終返回有效值。

如果要讓您的方法拋出ArgumentNullException而不是記錄並吞下它,請嘗試以下操作:

private static bool FilePathHasInvalidChars(string userInputPath)
{
    try
    {
        Path.GetFullPath(userInputPath);//this is where the warning appears

    }
    catch (ArgumentNullException) {
        Log.Error("The Program failed to run due to a null string value for the Input Directory.");
        throw;
    }
    catch (Exception e)
    {
        Log.Error(String.Format(
            "The Program failed to run due to invalid characters or empty string value for the Input Directory. Full Path : <{0}>. Error Message : {1}.",
            userInputPath, e.Message), e);
        return true;

    }
    return false;
}

進行此修改后,如果userInputPath為null,則您的方法將記錄並重新引發ArgumentNullException,並且您的單元測試將看到異常並通過。

您的代碼不會引發ArgumentNullException 根據您的代碼,它永遠不會*拋出任何異常-它應該只返回truefalse

將測試更改為等效於以下內容的NUnit:

Assert.IsTrue(() => sut.FilePathHasInvalidChars(testDirectory));

或者,如果一個空字符串應該拋出ArgumentNullException,則將代碼修改為類似以下內容的**:

private static bool FilePathHasInvalidChars(string userInputPath)
{
    if(string.IsNullOrWhiteSpace(userInputPath) throw new
        ArgumentNullException("userInputPath");
    try
    {
        Path.GetFullPath(userInputPath);//this is where the warning appears

    }
    catch (ArgumentException e)
    {
        Log.Error(String.Format(
            "The Program failed to run due to invalid characters or empty string value for the Input Directory. Full Path : <{0}>. Error Message : {1}.",
            userInputPath, e.Message), e);
        throw;    
    }
    catch (Exception e)
    {
        Log.Error(String.Format(
            "The Program failed to run due to invalid characters or empty string value for the Input Directory. Full Path : <{0}>. Error Message : {1}.",
            userInputPath, e.Message), e);
        return true;

    }
    return false;
}

*-對於“從不”的值,表示“因此很少您不必考慮它”

**-我沒有嘗試編譯它,因此可能會出現錯誤; 這只是一個起點。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM