I'm a bit stuck with this code (this is a sample):
public async Task Fail()
{
await Task.Run(() => { throw new Exception(); });
}
[Test]
public async Task TestFail()
{
Action a = async () => { await Fail(); };
a.ShouldThrow<Exception>();
}
The code doesn't catch the exception, and fails with
Expected a System.Exception to be thrown, but no exception was thrown.
I'm sure I'm missing something, but docs seem to suggest this is the way to go. Some help would be appreciated.
With Fluent Assertions v5+ the code will be like :
This should work.
You should use
Func<Task>
instead ofAction
:That will call the following extension which is used to verify asynchronous methods
Internally this method will run task returned by
Func
and wait for it. Something likeNote that test itself is synchronous.