2015-08-12 15 views
6

Sadece Assert.Catch veya Assert'in kullanılmasının uygun olduğu bazı örneklere bakıyorum. Birim testinde atılan istisnaları iddia ettiği için. ExpectedException'ı da kullanabileceğimi biliyorum, ancak özellikle "Catch" ve "Throws" arasındaki farkı bilmeyi merak ediyorum. Teşekkürler!Assert.Catch veya Assert.Throws birim testinde kullanıldığında

cevap

9

documentation ilk satırı oldukça net görünüyor:

Assert.CatchAssert.Throws benzer ancak belirtilenden türetilmiştir bir istisna geçecek. Bir istisna o geçerli (çok eşdeğer catch blokta yakalanmış olacağını yani) olduğu belirtilen istisnadan türemiştir eğer

Yani Assert.Catch kullanın.

// Require an ApplicationException - derived types fail! 
Assert.Throws(typeof(ApplicationException), code); 
Assert.Throws<ApplicationException>()(code); 

// Allow both ApplicationException and any derived type 
Assert.Throws(Is.InstanceOf(typeof(ApplicationException), code); 
Assert.Throws(Is.InstanceOf<ApplicationException>;(), code); 

// Allow both ApplicationException and any derived type 
Assert.Catch<ApplicationException>(code); 

// Allow any kind of exception 
Assert.Catch(code); 
:

Assert.Throws belgelerine hem örnekleri sağlar

İlgili konular