Replacing [ExpectedException] in WinRT unit test library

Recently I needed to write an unit test for my Windows Store application, that would check if a code snippet caused an expected exception. Usually this is done in the following manner:

[TestMethod]
[ExpectedException(typeof(DivideByZeroException))]
public async Task Test()
{
  int result = ( new Calc() ).Divide( 3, 0 );
}

However, the Unit Test Library for WinRT lacks the ExpectedException attribute.

Luckily, it was replaced by a far more convenient and useful method Assert.ThrowsException :

[TestMethod]
public async Task Test()
{
  Assert.ThrowsException<DivideByZeroException>( () => { new Calc() ).Divide( 3, 0 );} );
}

You need to provide the type of expected exception as the type parameter of this method and then a lambda expression (or  Action  instance), that contains the code that should throw the exception.

Buy me a coffeeBuy me a coffee

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.