Fix TUnitAssertions0009 code fix for xUnit Assert.True/False with user message#4265
Fix TUnitAssertions0009 code fix for xUnit Assert.True/False with user message#4265
Conversation
…or user message Co-authored-by: thomhurst <30480171+thomhurst@users.noreply.github.com>
|
@copilot Do any other assertions need special handling for messages? |
Co-authored-by: thomhurst <30480171+thomhurst@users.noreply.github.com>
Yes, good catch! I found that Fixed in commit 0f5ab0a:
The other xUnit assertions ( |
There was a problem hiding this comment.
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 |
XUnitAssertionCodeFixProvider.cs, forTrue/Falsecases, the code usesactualvariable which incorrectly takes the userMessage as the 2nd argument instead of usingexpected(the condition as 1st argument)TrueandFalsecases inGetNewExpressionmethod to:Assert.That().Because(userMessage)when a user message is providedTrueandFalsecases inConvertLambdaBodyToTUnitAssertionsmethod for consistent handlingTrueandFalsewith and without user messagesFailandSkipassertions which were also broken (producingFail.Test()instead ofFail.Test(message))FailandSkipwith messagesOriginal prompt
TUnitAssertions0009invalid fix for xUnit.net'sAssert.TrueandAssert.Falsewith user message #3857💡 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.