問題
使用 ExpectedException 測試例外狀況
解決方法
當程式有些例外狀況是可預期的,或是例外狀況是在程式的規劃中,此時就會需要做到例外狀況的測試
測試實作如下:
[TestMethod()]
[ExpectedException(typeof(ArgumentException), "The data not exist in DB.")]
public void TestDataNotExist()
{
using (ProductServiceClient target = new ProductServiceClient())
{
DataInfo actual = target.QueryData(“testdata”);
target.Close();
}
}
解釋:焦點請擺在
[ExpectedException(typeof(ArgumentException), "The data not exist in DB.")][ExpectedException(typeof(ArgumentException)]//若拋出為ArgumentException的時候,則會符合此條件
沒有留言:
張貼留言