Expected Exception in Unit test

17/10/2012 16:36

//Exception in calling target (test itself)

[TestMethod, Isolated]

[ExpectedException(typeof(EmailNotSentException))]

public void ProcessWhenGenericExceptionIsThrownConvertItToEmailNotSentException()

{

Isolate.WhenCalled(() => SPUtility.SendEmail(null, true, false, address, subject, body)).WillThrow(new Exception());

target.Process(data); //here is expected exception

}

//If we have try catch block with throw in code, we need to have try catch in unit test to catch it.

//If we expect exception without throw, we just use .WillThrow(new SPException()) in fake method.