fluentassertions.analyzers icon indicating copy to clipboard operation
fluentassertions.analyzers copied to clipboard

AssertIsTrue: Simplify Assertion does not offer automatic fix to apply

Open robertMSFTE opened this issue 2 years ago • 3 comments

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);
        }
    }
}

image

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.

robertMSFTE avatar Apr 04 '22 22:04 robertMSFTE

I think the fix here should be to not trigger the analyzer since asserting a constant value does not do anything

Meir017 avatar Apr 05 '22 17:04 Meir017

@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;
        }
    }
}

robertMSFTE avatar Apr 06 '22 20:04 robertMSFTE

Now this is interesting 🤔

Meir017 avatar Apr 06 '22 20:04 Meir017

Today I got this (which probably is related to why the fix does not work): image

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 avatar Nov 21 '22 16:11 robertMSFTE

@robertMSFTE can you share a minimal code sample to reproduce this error?

Meir017 avatar Nov 23 '22 05:11 Meir017

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)

robertMSFTE avatar Nov 25 '22 10:11 robertMSFTE

I'm seeing a similar issue for xUnit.

image

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);
    }
}

MisinformedDNA avatar May 22 '23 17:05 MisinformedDNA

resolved by #167

Meir017 avatar May 27 '23 20:05 Meir017

@Meir017 The scenario still reproduces on 0.20.0 - aka. there is no fix suggestion.

robertMSFTE avatar Jun 19 '23 11:06 robertMSFTE

@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:

image

While others do:

image

Running inside Rider IDE 2023.2.2 and using .NET 7.0.401 (OSX 14.0)

MS-Test Version:

image

kimpenhaus avatar Oct 02 '23 07:10 kimpenhaus

@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 🙏🏼

ConsoleApp2.zip

kimpenhaus avatar Oct 02 '23 15:10 kimpenhaus

@kimpenhaus @robertMSFTE please check in version 0.25.0

Meir017 avatar Oct 03 '23 06:10 Meir017

@Meir017

yay - that looks good - thanks for the quick fix

image

kimpenhaus avatar Oct 03 '23 08:10 kimpenhaus