簡體   English   中英

如何處理NUnit中的拋出異常

[英]How to handle thrown exception in NUnit

我已經在C#中為我的MVC項目編寫了一個單元測試類。

測試方法如下

 [Test]
    public void To_Add_DocumentStatusIsNull_ThrowsInvalidOperationException_ServiceTest()
    {
        try
        {

        _IDocumentStatusRepositoryMock = new Mock<IDocumentStatusRepository>();
        _unitOfWorkMock = new Mock<IUnitOfWork>();

        DocumentStatusService documentStatusService = new  
         DocumentStatusService(_unitOfWorkMock.Object,  
          _IDocumentStatusRepositoryMock.Object); 

        DocumentStatus documentStatus;
        documentStatus = null;

        _IDocumentStatusRepositoryMock.Setup(m => m.Add(documentStatus));
        documentStatusService.Add(documentStatus);

        Assert.Pass();

        }
        catch (Exception e)
        {
            throw new Exception(e.Message);
        }
    }

服務方法如下

   public virtual void Add(TEntity entity)
    {
        try
        {

        if (entity == null)
        {
            throw new ArgumentNullException("entity");
        }
        _repository.Add(entity);

        }
        catch (Exception e)
        {
            throw new Exception(e.Message);
        }
    }

現在這個測試方法只是因為服務類拋出而沒有傳遞ArgumentNullException.So如何處理ArgumentNullException或如何讓這個測試通過?

請任何人幫忙

如果您正在嘗試檢查ArgumentNullException是否正常工作(其中:它當前不是)。 那聽起來像你想要的:

[Test, ExpectedException(typeof(ArgumentNullException), ExpectedMessage = @"Value cannot be null.
Parameter name: entity")]
public void To_Add_DocumentStatusIsNull_ThrowsInvalidOperationException_ServiceTest()
{
    _IDocumentStatusRepositoryMock = new Mock<IDocumentStatusRepository>();
    _unitOfWorkMock = new Mock<IUnitOfWork>();

    DocumentStatusService documentStatusService = new  
     DocumentStatusService(_unitOfWorkMock.Object,  
      _IDocumentStatusRepositoryMock.Object); 

    DocumentStatus documentStatus;
    documentStatus = null;

    _IDocumentStatusRepositoryMock.Setup(m => m.Add(documentStatus));
    documentStatusService.Add(documentStatus);
}

...

public virtual void Add(TEntity entity)
{
    if (entity == null)
    {
        throw new ArgumentNullException("entity");
    }
    _repository.Add(entity);
}

我假設:看看代碼,這個單元測試不應該通過。 在大多數情況下,向列表添加NULL不是預期的行為。

我看到兩個選項:A)你應該為你測試metod添加一個try / catch。

try 
{
    _IDocumentStatusRepositoryMock.Setup(m => m.Add(documentStatus));
    documentStatusService.Add(documentStatus);
}
catch (Exception )
{
    Assert.Fail(); // or nothing is expected behaviour
}

B)從測試方法中刪除try / catch塊,這樣就不會吞下異常。 (每個沒有失敗的測試或者Assert或者unhandeled異常會自動通過)

測試ArgumentNullException

如果你刪除了不明智的

catch (Exception e)
{
   throw new Exception(e.Message);
}

從您要測試的代碼(當前catch失去錯誤的上下文,並打破堆棧跟蹤,見下文),您的測試可以像在Assert.Throws<ArgumentNullException>()包裝調用一樣簡單Assert.Throws<ArgumentNullException>()

[Test]
public void PassingANullEntityToAddMustThrowArgumentNullException()
{
    var documentStatusService = new  DocumentStatusService(...); 
    Assert.Throws<ArgumentNullException>(() =>  documentStatusService.Add(null));
}

Re:你的異常處理程序

在您的服務代碼中,永遠不會捕獲異常並重新拋出它,因為這將丟失堆棧跟蹤(例如_repository.Add(entity);也可以拋出。)。 您也沒有通過拋出e.Message來添加任何值,因為它已經在原始異常中(包含堆棧跟蹤和內部異常等附加信息)

壞:

  catch (Exception e)
  {
      throw new Exception(e.Message);
  }

更好:如果您捕獲並重新拋出某些值,請將原始內容包裝為內部異常:

 catch (SqlException ex)
 {
    throw new Exception("Some value add here", ex);
 }

或者如果你只是攔截並允許傳播:

 catch (SqlException)
 {
    // Do some logging
    throw;
 }

除非您添加值或處理它,否則我最好讓異常傳播。

暫無
暫無

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

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