cezarypiatek / RoslynTestKit

A lightweight framework for writing unit tests for Roslyn diagnostic analyzers, code fixes, refactorings and completion providers.
Other
24 stars 7 forks source link

Plain and simple test complaining netstandard 2.0.0 is missing? #31

Open ShaharPrishMSFT opened 5 months ago

ShaharPrishMSFT commented 5 months ago

When trying to run a simple test, I am told that 2.0.0 is not referenced and the test fails.

The analyzer is written in netstandard (which I think is a requirement)

I also have a netstandard sattelite assembly containing attributes (that's where RequireUsingAttribute is coming from)

    [Fact]
    public void TestMisuseOfRequireAttribute()
    {
        const string code = """
            using SubtleEngineering.Analyzers.Decorators;
            [RequireUsing]
            public class [|MyClass|]
            {
            }
            """;
        var sut = CreateSut();

        sut.HasDiagnostic(code, DiagnosticIds.TypesDecoratedWithTheRequireUsingAttributeMustInheritFromIDisposable);
    }

    public AnalyzerTestFixture CreateSut()
    {
        var fixture = RoslynFixtureFactory.Create<RequireUsingAnalyzer>(
            new AnalyzerTestFixtureConfig()
            {
                References = [
                    ReferenceSource.FromType<RequireUsingAttribute>(),
                    // ReferenceSource.FromType<Attribute>(),
                    ],
            });

        return fixture;
    }

I am getting the following test error:

RoslynTestKit.RoslynTestKitException : Input document contains errors: TestDocument0(2,2): error CS0012: The type 'Attribute' is defined in an assembly that is not referenced. You must add a reference to assembly 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.

Stack Trace:  AnalyzerTestFixture.GetDiagnostics(Document document) AnalyzerTestFixture.HasDiagnostic(Document document, String diagnosticId, IDiagnosticLocator locator) AnalyzerTestFixture.HasDiagnostic(String markupCode, String diagnosticId) RequireUsingAnalyzerTests.TestMisuseOfRequireAttribute() line 20 RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor) MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)

ShaharPrishMSFT commented 5 months ago

I found ReferenceSource.NetStandard2_0

After adding it, I am getting the following:

RoslynTestKit.RoslynTestKitException : Input document contains errors: TestDocument0(2,2): error CS0012: The type 'Attribute' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Runtime, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.

ShaharPrishMSFT commented 5 months ago

Still not working after adding all three:

        var fixture = RoslynFixtureFactory.Create<RequireUsingAnalyzer>(
            new AnalyzerTestFixtureConfig()
            {
                References = [
                    ReferenceSource.FromType<RequireUsingAttribute>(),
                    ReferenceSource.FromType<Attribute>(),
                    ReferenceSource.NetStandard2_0,
                    ],
            });

Error is:

Message:  RoslynTestKit.RoslynTestKitException : Input document contains errors: TestDocument0(2,2): error CS0012: The type 'Attribute' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Runtime, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.

Stack Trace: 

cezarypiatek commented 5 months ago

What's the target framework of test project?