2011-11-23 22 views
5

Birden çok özel durum için bir TestMethod istiyorum. Sorun, Testmethod ilk atılan istisnadan sonra durur.UnitTest ExpectedException çoklu istisnalar ile

Ben böyle bir şey yapabileceğini biliyoruz:

try 
{ 
    sAbc.ToInteger(); 
    Assert.Fail(); // If it gets to this line, no exception was thrown 
} 
catch (ArgumentException) { } 

Ama Aşağıdaki kod tabanını kullanmak istiyorum:

[TestMethod, ExpectedException(typeof(ArgumentException), "...")] 
public void StringToIntException() 
{ 
    sAbc.ToInteger(); // throws an exception and stops here 
    sDecimal.ToInteger(); // throws theoretically a exception too... 
} 

Ve ben bir TestMethod oluşturmak istemiyoruz Her olası istisna bunun gibi:

[TestMethod, ExpectedException(typeof(ArgumentException), "...")] 
public void StringToIntException() 
{ 
    sAbc.ToInteger(); 
} 

[TestMethod, ExpectedException(typeof(ArgumentException), "...")] 
public void StringToIntException() 
{ 
    sDecimal.ToInteger(); 
} 

cevap

0

Bildiğim kadarıyla, Öznitelikler ile imkansız çünkü ption atılır, yöntemlerin uygulanması kesilir. Bu nedenle, ilk satırda bir istisna varsa, ikincisi yürütülmez.

Assert.Throws<Exception>(delegate { /*Your code*/ }); 
+0

nasıl ya-tipi çoklu istisnaları: Gerçekten özellik gerekiyorsa

sahiptir nUnit kullanılır? Bir seferde sadece bir istisna olacak. Bu mstest tarafından elde edilebilir mi? – liang