Alex Rivera | Logout

MOQ - verify exception was thrown

Asked 2013-04-17T06:52:38.600
62

I working with MOQ framework for my testing. I have a scenario in which I expect a fault exception to be thrown. How can I verify it was thrown?

public void Koko(List<string?> list) 
{ 
   foreach(string? str in list) 
   { 
        if (str != null) someProperty.Foo(str); 
        else throw new FormatException(); 
   } 
} 
Edit
Report

1 Answer

12

You can test that an Exception is thrown using NUnit Asserts:

Assert.That(() => testObject.methodToTest(), Throws.TypeOf<FaultException>());
answered 2013-04-17T07:00:58.470

Your Answer