Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jan 8, 2026

  • Explore the repository and understand the issue
  • Identify root cause: In XUnitAssertionCodeFixProvider.cs, for True/False cases, the code uses actual variable which incorrectly takes the userMessage as the 2nd argument instead of using expected (the condition as 1st argument)
  • Fix the True and False cases in GetNewExpression method to:
    1. Use the first argument (condition) in Assert.That()
    2. Append .Because(userMessage) when a user message is provided
  • Fix the True and False cases in ConvertLambdaBodyToTUnitAssertions method for consistent handling
  • Add tests to verify the fix for both True and False with and without user messages
  • Run tests to validate the fix
  • Run code review (no issues found)
  • Fix Fail and Skip assertions which were also broken (producing Fail.Test() instead of Fail.Test(message))
  • Add tests for Fail and Skip with messages
  • All 13 tests passing on net8.0 and net9.0
Original prompt

This section details on the original issue you should resolve

<issue_title>TUnitAssertions0009 invalid fix for xUnit.net's Assert.True and Assert.False with user message</issue_title>
<issue_description>Hello,

The analyzer fix for true/false assertion with a user message is not correct:

Xunit.Assert.True(condition, userMessage)
Xunit.Assert.False(condition, userMessage)

e.g.:

public class TrueFalseTest
{
    [Test]
    public void TrueTest()
    {
        bool result = true;
        Xunit.Assert.True(result, "user message if false");
    }

    [Test]
    public void FalseTest()
    {
        bool result = true;
        Xunit.Assert.False(result, "user message if false");
    }
}

Actual behavior

public class TrueFalseTest
{
    [Test]
    public void TrueTest()
    {
        bool result = true;
        Assert.That("user message if false").IsTrue();
    }

    [Test]
    public void FalseTest()
    {
        bool result = false;
        Assert.That("user message if false").IsFalse();
    }
}

Expected behavior

public class TrueFalseTest
{
    [Test]
    public void TrueTest()
    {
        bool result = true;
        Assert.That(result).IsTrue().Because("user message if false");
    }

    [Test]
    public void FalseTest()
    {
        bool result = false;
        Assert.That(result).IsFalse().Because("user message if true");
    }
}

</issue_description>

Comments on the Issue (you are @copilot in this section)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

…or user message

Co-authored-by: thomhurst <30480171+thomhurst@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix invalid fix for xUnit.net Assert.True and Assert.False Fix TUnitAssertions0009 code fix for xUnit Assert.True/False with user message Jan 8, 2026
Copilot AI requested a review from thomhurst January 8, 2026 12:45
@thomhurst
Copy link
Owner

@copilot Do any other assertions need special handling for messages?

Co-authored-by: thomhurst <30480171+thomhurst@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Jan 8, 2026

@copilot Do any other assertions need special handling for messages?

Yes, good catch! I found that Fail and Skip were also broken - they were generating Fail.Test() and Skip.Test() without the required message/reason argument.

Fixed in commit 0f5ab0a:

  • Assert.Fail("message")Fail.Test("message")
  • Assert.Skip("reason")Skip.Test("reason")

The other xUnit assertions (Equal, NotEqual, Null, NotNull, Contains, Same, NotSame, Empty, NotEmpty, IsType, Throws, etc.) don't have user message overloads in xUnit's API.

@thomhurst thomhurst marked this pull request as ready for review January 8, 2026 15:15
Copilot AI review requested due to automatic review settings January 8, 2026 15:15
@thomhurst thomhurst enabled auto-merge (squash) January 8, 2026 15:15
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes the TUnitAssertions0009 code fix provider that was incorrectly converting xUnit's Assert.True/Assert.False assertions when a user message was provided. The fix also addresses similar issues with Assert.Fail and Assert.Skip assertions.

Key Changes:

  • Fixed True/False assertions to use the condition (first argument) instead of the message (second argument) as the assertion subject
  • Added proper .Because(message) suffix when user messages are provided
  • Fixed Fail/Skip assertions to include the message parameter in the generated code

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
TUnit.Assertions.Analyzers.CodeFixers/XUnitAssertionCodeFixProvider.cs Fixed True/False/Fail/Skip code fix logic in GetNewExpression method; added CreateBooleanAssertion helper; enhanced ConvertLambdaBodyToTUnitAssertions to handle messages
TUnit.Assertions.Analyzers.CodeFixers.Tests/XUnitAssertionCodeFixProviderTests.cs Added 6 comprehensive test cases covering True/False/Fail/Skip with and without messages

@thomhurst thomhurst merged commit 1fa2a97 into main Jan 8, 2026
18 of 19 checks passed
@thomhurst thomhurst deleted the copilot/fix-invalid-assertions-fix branch January 8, 2026 15:43
This was referenced Jan 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TUnitAssertions0009 invalid fix for xUnit.net's Assert.True and Assert.False with user message

2 participants