問題
使用 Assert.AreEqual 測試例外狀況所傳回的訊息是否正確
解決方法
當我們需要測試的程式需要測試到以下狀況:
1.如所傳入參數錯誤時應該回傳甚麼樣的錯誤訊息
2.傳入固定格式參數應該會回傳怎樣的制定訊息
這時候用這篇所以提到的測試例外訊息又不同了,因為單純的只用拋出例外訊息去判斷測試是否正確在此時變得太攏統
因此可以改用以下的方式:
[TestMethod()]
public void TestDataNotExist()
{
string expectedMsg = "The input argument is null or empty value.";
string actulMsg = string.Empty; ;
ProductServiceClient target = new ProductServiceClient();
try
{
target.QueryData(null);
}
catch (FaultException ex)
{
actulMsg = ex.Message;
}
target.Close();
Assert.AreEqual(expectedMsg, actulMsg, true, "Excepted exception message diff.");
}
以上的方式是將例外發生時,預期應該傳回的訊息與實際傳回的訊息做比較,用來確認所回傳的訊息是否如預期一般
沒有留言:
張貼留言