Skip to content

xUnit Migration: Assert.Throws converted to undefined 'awaitAssert.ThrowsAsync' #4331

@thomhurst

Description

@thomhurst

Description

When using the TUnit xUnit migration code fixer (TUXU0001), the fixer incorrectly converts Assert.Throws<T>() to awaitAssert.ThrowsAsync<T>() where awaitAssert is an undefined variable, resulting in CS0103 compilation errors.

Steps to Reproduce

  1. Create an xUnit test with Assert.Throws<T>():
[Fact]
public void TestThrows()
{
    var ex = Assert.Throws<ArgumentException>(() => DoSomething());
    Assert.Equal("param", ex.ParamName);
}
  1. Install TUnit package and run dotnet format analyzers --severity info --diagnostics TUXU0001

Expected Behavior

The code should be converted to valid TUnit syntax, such as:

[Test]
public async Task TestThrows()
{
    await Assert.That(() => DoSomething()).ThrowsAsync<ArgumentException>();
    // Or similar valid TUnit pattern
}

Actual Behavior

The code is converted to:

[Test]
public async Task TestThrows()
{
    var ex = awaitAssert.ThrowsAsync<ArgumentException>(() => DoSomething());
    await Assert.That(ex.ParamName).IsEqualTo("param");
}

Where awaitAssert is never defined, causing:

error CS0103: The name 'awaitAssert' does not exist in the current context

Example from Moq Test Suite

Original xUnit code:

[Fact]
public void Custom_matcher_property_appears_by_name_in_verification_error_message()
{
    var child = new Mock<IChild>();
    child.Setup(c => c.PlayWith(Toy.IsRed)).Verifiable();

    var ex = Assert.Throws<MockException>(() => child.Verify());

    Assert.Contains(".PlayWith(CustomMatcherFixture.Toy.IsRed)", ex.Message);
}

Converted (incorrectly):

[Test]
public async Task Custom_matcher_property_appears_by_name_in_verification_error_message()
{
    var child = new Mock<IChild>();
    child.Setup(c => c.PlayWith(Toy.IsRed)).Verifiable();

    var ex = awaitAssert.ThrowsAsync<MockException>(() => child.Verify());

    await Assert.That(ex.Message).Contains(".PlayWith(CustomMatcherFixture.Toy.IsRed)");
}

Impact

This affects every test that uses Assert.Throws<T>() pattern. In the Moq test suite, this resulted in 434+ compilation errors.

Environment

  • TUnit Version: 1.9.91
  • .NET SDK: 10.0.101
  • OS: Windows

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions