alefranz / MELT

MELT is a free, open source, testing library for the .NET Standard Microsoft Extensions Logging library. It is a solution to easily test logs.
Apache License 2.0
74 stars 5 forks source link

MissingMethodException on using LoggingAssert Contains #37

Closed Dorus closed 1 year ago

Dorus commented 1 year ago

I updated my xUnit packages today, and got this exception:

System.MissingMethodException: Method not found: 'Void Xunit.Sdk.EqualException..ctor(System.Object, System.Object)'.
   at Xunit.LoggingAssert.Contains(IEnumerable`1 expectedValues, IEnumerable`1 actualValues)
   at Xunit.LoggingAssert.Contains(String key, Object value, IEnumerable`1 actualValues)

on calling this code:

var log = _loggerFactory.Sink.LogEntries.First();
LoggingAssert.Contains("fileName", "bla.txt", log.Properties);
alefranz commented 1 year ago

Thanks for the report, I'll have a look!

Dorus commented 1 year ago

I ended up doing the check with FluentAssertions like this:

    var log = _loggerFactory.Sink.LogEntries.Select(e => e.Properties).Should().BeEquivalentTo(new[] {
      new Dictionary<string, string>
      {
        { "{OriginalFormat}", "File {fileName} is not an xml file." },
        { "fileName", "bla.txt" }
      }
    });
b-straub commented 1 year ago

Same for me 2.4.2 working , 2.5.0 not. Testing on .NET8.0 latest preview with async tests.

bemayr commented 1 year ago

@alefranz Maybe the following can help your investigation a bit:

I just had the same problem while debugging Roslyn Analyzer Tests and found out that between xUnit 2.4.2 and 2.5.0 the public interface of EqualException changed. This diff clearly shows the difference in the constructor, which leads to the MissingMethodException.

For MELT, the problem is in those lines: https://github.com/alefranz/MELT/blob/b4fbc549609d35266b932d6beabbde2f2a47c7f0/src/MELT.Xunit/LoggingAssert.cs#L53-L55

It looks like

EqualException.ForMismatchedValues(expected, actual)

is the new way to construct such an Exception now.


As a comparison the Roslyn Analyzer Testing problem stems from: https://github.com/dotnet/roslyn-sdk/blob/42ce3944fc4488a7ebfbf1f8d395c66808577d77/src/Microsoft.CodeAnalysis.Testing/Microsoft.CodeAnalysis.Testing.Verifiers.XUnit/EqualWithMessageException.cs#L12-L16

alefranz commented 1 year ago

Released 0.9.0. Any feedback is appreciated.

Thanks for the investigation @bemayr . I went the easy route for now and built the base exception, manually crafting the message. Given the discussion on https://github.com/dotnet/roslyn-sdk/issues/1099, it seems like the best solution is to drop the dependency to the assert package. I have some refactoring in progress, so will consider dropping it as part of that.