测试源生成器找不到 Xunit.Sdk.EqualException

Testing source generator can't find Xunit.Sdk.EqualException

提问人:Gargoyle 提问时间:10/9/2023 最后编辑:Gargoyle 更新时间:10/23/2023 访问量:41

问:

我正在尝试测试我的 .NET 7 C# 源代码生成器,但是当测试运行时,我不断收到此异常:IIncrementalGenerator

System.MissingMethodException:找到方法 nout:“Void Xunit.Sdk.EqualException..ctor(System.Object, System.Object'.

我无法弄清楚我错过了什么,因为似乎我正在将当前程序集的引用加载到编译中。

我正在使用以下类来运行测试:

public class SourceGeneratorVerifier : SourceGeneratorTest<XUnitVerifier> {
    public override string Language => LanguageNames.CSharp;

    protected override CompilationOptions CreateCompilationOptions() => new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary);

    protected override GeneratorDriver CreateGeneratorDriver(Project project, ImmutableArray<ISourceGenerator> sourceGenerators) => CSharpGeneratorDriver.Create(sourceGenerators, project.AnalyzerOptions.AdditionalFiles, (CSharpParseOptions)project.ParseOptions!, project.AnalyzerOptions.AnalyzerConfigOptionsProvider);

    protected override ParseOptions CreateParseOptions() => new CSharpParseOptions(LanguageVersion.Default, DocumentationMode.Diagnose);

    protected override IEnumerable<ISourceGenerator> GetSourceGenerators() { yield return new LampCodeGenerator().AsSourceGenerator(); }

    protected override string DefaultFileExt => "cs";

    public static Task RunTestAsync(string source, string expected, params string[] path) {
        var assemblies = new ReferenceAssemblies("net7.0", new PackageIdentity("Microsoft.NETCore.App.Ref", "7.0.0"), Path.Combine("ref", "net7.0"));

        var test = new SourceGeneratorVerifier {
            TestState = {
                ReferenceAssemblies = assemblies,
                Sources = {
                    source
                },
                GeneratedSources = {
                    ($"GeneratorAttributes.g.cs", "...."),
                    (Path.Combine(path), expected)
                }
            }
        };

        test.TestState.AdditionalReferences.Add(typeof(SourceGeneratorVerifier).Assembly);

        return test.RunAsync();
    }
}

我的测试项目包含以下参考资料:

  <ItemGroup>
    <PackageReference Include="Microsoft.CodeAnalysis.CSharp.SourceGenerators.Testing.XUnit" Version="1.1.1" />
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
    <PackageReference Include="FluentAssertions" Version="6.12.0" />
    <PackageReference Include="xunit" Version="2.5.1" />
    <PackageReference Include="xunit.runner.visualstudio" Version="2.5.1">
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
    <PackageReference Include="coverlet.collector" Version="6.0.0">
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.CodeAnalysis.CSharp.Analyzer.Testing.XUnit" Version="1.1.1" />
    <PackageReference Include="Microsoft.CodeAnalysis.CSharp.CodeFix.Testing.XUnit" Version="1.1.1" />
    <PackageReference Include="Microsoft.CodeAnalysis.CSharp.CodeRefactoring.Testing.XUnit" Version="1.1.1" />
    <PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.7.0" PrivateAssets="all" />
  </ItemGroup>
. XUnit NET-7.0 源生成器

评论


答:

3赞 Alex Groza 10/23/2023 #1

有同样的问题。 似乎可能与 https://github.com/dotnet/roslyn-sdk/issues/1099 有关

请尝试使用此解决方法 https://github.com/dotnet/roslyn-sdk/issues/1099#issuecomment-1723487931

评论

0赞 JasonS 11/20/2023
我也被这个咬了一口。提到的修复程序(不要使用 XUnit 包)对我有用。