fluentassertions.analyzers
fluentassertions.analyzers copied to clipboard
AssertIsTrue: Simplify Assertion does not offer automatic fix to apply
Description
AssertIsTrue: Simplify Assertion does not offer automatic fix to apply.
Complete minimal example reproducing the issue
This gets a suggestion but there is no fix shown to apply in VS.
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace TestProject1
{
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
Assert.IsTrue(true);
}
}
}
Expected behavior:
Fix suggestion should be shown.
Actual behavior:
Fix suggestion is not shown.
Versions
- Which version of Fluent Assertions Analyzers are you using? FluentAssertions.Analyzers 0.17.2
- Which .NET runtime and version are you targeting? .NET Core 3.1.
I think the fix here should be to not trigger the analyzer since asserting a constant value does not do anything
@Meir017 This was just an example. The issue is that in general the fix never seems to be shown for this rule.
Example without a constant:
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace TestProject1
{
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
Assert.IsTrue(ComputeSomething());
}
private bool ComputeSomething()
{
return true;
}
}
}
Now this is interesting 🤔
Today I got this (which probably is related to why the fix does not work):
System.InvalidCastException : Unable to cast object of type 'Microsoft.CodeAnalysis.CSharp.Syntax.ExpressionStatementSyntax' to type 'Microsoft.CodeAnalysis.CSharp.Syntax.ExpressionSyntax'. at async FluentAssertions.Analyzers.FluentAssertionsCodeFixProvider.RegisterCodeFixesAsync(<Unknown Parameters>) at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at async Microsoft.CodeAnalysis.CodeFixes.CodeFixService.GetCodeFixesAsync(<Unknown Parameters>) at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at async Microsoft.CodeAnalysis.Extensions.IExtensionManagerExtensions.PerformFunctionAsyncT
@robertMSFTE can you share a minimal code sample to reproduce this error?
I saw this only once so far. Will add a repro if I get this to reproduce. (But the general behavior explained at the beginning is always there)
I'm seeing a similar issue for xUnit.
I get a recommendation outside of a block, but not inside:
[Fact]
public void SomeTest()
{
bool result = false;
Assert.True(result);
if (true)
{
Assert.True(result);
}
while (true)
{
Assert.True(result);
}
}
resolved by #167
@Meir017 The scenario still reproduces on 0.20.0
- aka. there is no fix suggestion.
@Meir017 we are running with FluentAssertions.Analyzers 0.24.0
and having the same behavior
Àssert.IsTrue / Assert.IsFalse
only shows the suggestion but not the quix fix:
While others do:
Running inside Rider IDE 2023.2.2 and using .NET 7.0.401 (OSX 14.0)
MS-Test Version:
@Meir017 I could reporduce this with the simplest Test possible - not sure if that helps you but I've attached that as an sample. First I thought it might some sort of constellation with other analyzers or a more complex environment but it seems it also happens with only the things needed to run the tests under MSTest.
I appreciate the time you'll spend - thanks for that 🙏🏼
@kimpenhaus @robertMSFTE please check in version 0.25.0
@Meir017
yay - that looks good - thanks for the quick fix