2012-02-22 73 views
1

我有MS Test單元測試,確保在測試方法給出錯誤參數時引發Exception。我正在使用的模式:VS2010調試器破壞處理異常

My actual; 
bool threw = false; 
try 
{ 
    actual = target.DoSomething(aBadParameter); 
} 
catch 
{ 
    threw = true; 
} 

Assert.IsTrue(threw); 

我有CLR異常設置爲只有當用戶未處理(不是當拋出)時打破。但是,當DoSomething()引發new Exception()時,調試器會中斷。如果我繼續,單元測試成功完成。

如果我將單元測試代碼剪切並粘貼到主程序中並在主程序的上下文中運行(而不是在MS Test下),則調試程序不會在用戶處理的異常中斷。

如何防止調試器破壞用戶處理的異常?

這並不因爲在這種情況下,有關

Getting an Unhandled Exception in VS2010 debugger even though the exception IS handled

表面上出現異常被拋出在不同的線程,並正在由一個回調裏面的CLR重新拋出。

回答

2

的慣用方法來測試在MSTest中拋出的異常使用ExpectedException屬性:

[TestMethod] 
[ExpectedException(typeof(FooException))] 
public void ThrowsFooExceptionWithBadInput() 
{ 
    var actual = target.DoSomething(aBadParameter); 
} 
+0

我正要鍵入這一個在+1 – 2012-02-22 22:31:28

+0

我得到完全用的ExpectedException相同的行爲。請參閱http://stackoverflow.com/questions/2628965/expectedexception-on-testmethod-visual-studio-2010 – 2012-02-22 22:34:57

+0

@Eric J:剛注意到這可能沒有解決您的問題中的主要問題 - 即使異常是處理。讓我知道這是否有幫助,否則將刪除。 – BrokenGlass 2012-02-22 22:35:48