What happened to Assert.DoesNotThrowAsync() in xUnit?

C#Xunit

C# Problem Overview


I migrated my unit test project from version 2.0.0-beta-{something} to 2.0.0 (stable) through NuGet. It seems like Assert.DoesNotThrowAsync() is not available anymore.

For Example:

[Fact]
public void CanDeleteAllTempFiles() {
    Assert.DoesNotThrowAsync(async () => DocumentService.DeleteAllTempDocuments());
}

Results in

> DocumentServiceTests.cs(11,11): Error CS0117: 'Xunit.Assert' does not contain a definition for 'DoesNotThrowAsync' (CS0117)

A workaround would be to omit the test. Is there any better solution?

C# Solutions


Solution 1 - C#

I just wanted to update the answer with current information (Sep 2019).

As Malcon Heck mentioned, using the Record class is preferred. Looking at xUnit's Github, I see that a current way to check for lack of exceptions thrown is like this

[Fact]
public async Task CanDeleteAllTempFiles() {
    var exception = await Record.ExceptionAsync(() => DocumentService.DeleteAllTempDocuments());
    Assert.Null(exception);
}

Solution 2 - C#

As you can see in this discussion, the recommended way to test if a method does not throw in xUnit v2 is to just call it.

In your example, that would be:

[Fact]
public async Task CanDeleteAllTempFiles() {
    await DocumentService.DeleteAllTempDocuments();
}

Solution 3 - C#

OP is asking about async, but if anyone else got here looking for non-async equivalent then:

[Fact]
public void TestConstructorDoesNotThrow()
{
	var exception = Record.Exception(() => new MyClass());
	Assert.Null(exception);
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionder_michaelView Question on Stackoverflow
Solution 1 - C#slaskyView Answer on Stackoverflow
Solution 2 - C#Marcio RinaldiView Answer on Stackoverflow
Solution 3 - C#RJFalconerView Answer on Stackoverflow