MSpec Catch.Exception doesn't work for async method?

52 views Asked by At

The following mspec test works as expected.

 static Exception exception;

 private Because of = () =>
     exception = Catch.Exception( () => throw new NotFoundException("test"));

 It should_throw_exception = () =>
     exception.ShouldBeOfExactType<NotFoundException>();

However, the following code fails the test because exception is null?

 private Because of = () =>
     exception = Catch.Exception(async () => throw new NotFoundException("test"));
1

There are 1 answers

0
Brad Lawrence On

I couldn't get it to work either, so I had to work around it. Rather than use their convenience method for catching exceptions, I used a block and caught the exception myself. This code is working for me:

static Exception exception;
Because of = async () =>
{
    try
    {
        throw new NotFoundException("test");
    }
    catch (Exception ex)
    {
        exception = ex;
    }
};