0

I try to test this async method:

public static async Task SimpleAsync() { await Task.Delay(10); throw new Exception("Should fail."); } 

with:

Assert.Throws<Exception>(async () => await AsyncUnderTest.SimpleAsync()); 

but NUnit doesn't run:

Async void methods are not supported

How should I test the exception in NUnit?
I wonder how to include the Task return value in the lambda expression?

This question is much clearer and educational then the chaotic so-called "duplicate" question.

1

1 Answer 1

-3

Indeed:

Assert.ThrowsAsync<Exception>(async Task () => await AsyncUnderTest.SimpleAsync()); 

or also:

Assert.ThrowsAsync<Exception>(async () => await AsyncUnderTest.SimpleAsync()); 

So async void is allowed in the lambda expression when using ThrowsAsync it is not allowed when using Throws.

Sign up to request clarification or add additional context in comments.

2 Comments

None of the lambdas in your answer is async void. They are both async Task.
the second one is async void

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.