GuOrg / Gu.Roslyn.Asserts

Asserts for testing Roslyn analyzers.
MIT License
21 stars 7 forks source link

[Question] - How do I handle usings in test code? #314

Open mwasson74 opened 1 year ago

mwasson74 commented 1 year ago

I am new to testing Roslyn things. I have the following test:

  [Fact]
  public void Test2()
  {
    const string code = @"
    namespace MyNamespace;
    using System;
    using Microsoft.AspNetCore.Components;
    class Test
    {
      [Inject] private Test ↓F { get; set; }
    }";

    RoslynAssert.Suppressed(_suppressor, code);
  }

And get the following result:

Expected and actual diagnostics do not match.
Matched: 1 diagnostic(s).
Missed:
  CS8019 Unnecessary using directive.
    at line 2 and character 4 in file Test.cs | ↓using System;
  CS0246 The type or namespace name 'System' could not be found (are you missing a using directive or an assembly reference?)
    at line 2 and character 10 in file Test.cs | using ↓System;
  CS8019 Unnecessary using directive.
    at line 3 and character 4 in file Test.cs | ↓using Microsoft.AspNetCore.Components;
  CS0246 The type or namespace name 'Microsoft' could not be found (are you missing a using directive or an assembly reference?)
    at line 3 and character 10 in file Test.cs | using ↓Microsoft.AspNetCore.Components;
  CS0518 Predefined type 'System.Object' is not defined or imported
    at line 4 and character 10 in file Test.cs | class ↓Test
  CS1729 'object' does not contain a constructor that takes 0 arguments
    at line 4 and character 10 in file Test.cs | class ↓Test
  CS0518 Predefined type 'System.Object' is not defined or imported
    at line 6 and character 7 in file Test.cs | [↓Inject] private Test F { get; set; }
  CS0246 The type or namespace name 'InjectAttribute' could not be found (are you missing a using directive or an assembly reference?)
    at line 6 and character 7 in file Test.cs | [↓Inject] private Test F { get; set; }
  CS0246 The type or namespace name 'Inject' could not be found (are you missing a using directive or an assembly reference?)
    at line 6 and character 7 in file Test.cs | [↓Inject] private Test F { get; set; }
  CS0518 Predefined type 'System.Object' is not defined or imported
    at line 6 and character 23 in file Test.cs | [Inject] private ↓Test F { get; set; }
  CS0518 Predefined type 'System.Void' is not defined or imported
    at line 6 and character 37 in file Test.cs | [Inject] private Test F { get; ↓set; }

How do I properly handle usings and other packages that I may need? The [Inject] attribute is from the AspNetCore.Components library which I have referenced in my test .csproj but there must be more to it.

Any guidance is greatly appreciated.

Thanks,

Matt

JohanLarsson commented 1 year ago

Try using this https://github.com/GuOrg/Gu.Roslyn.Asserts#settings

Maybe something like:

Settings.Default = Settings.Default.WithMetadataReferences(
    Asserts.MetadataReferences.Transitive(typeof(Path.To.InjectAttribute)));

It adds default metadata references for all tests. You can also pass metadata references per assert but it tends to produce noisy tests