Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Any type loaded from assembly at runtime does not match compile-time type #1069

Closed
bhaeussermann opened this issue Feb 28, 2023 · 4 comments
Closed

Comments

@bhaeussermann
Copy link

This will affect any test that loads at runtime an assembly containing classes that implement specific interface types (as in plugin system / dependency injection). The interface type implemented by the class will not pass equality against a compile-time reference to that type.

  • NUnit and NUnit3TestAdapter versions: NUnit: 3.13.3, NUnit3TestAdapter: 4.4.0 (works fine with 4.3.1)
  • Visual Studio edition and full version number: VS Enterprise 2022, 17.4.1
  • A short repro, preferably attached or pointing to a git repo or gist: See below
  • What .net platform and version is being targeted: .NET 6.0

Steps to reproduce

  • Create a "Specification" class project containing an interface called IService:
public interface IService
{
    string Hello();
}
  • Create an "Implementation" class project referencing the Specification project with a class called Service:
public class Service : IService
{
    public string Hello() => "Hi";
}
  • Create an NUnit test project referencing the Specification project.
  • Add the following test class:
[TestFixture]
public class Tests
{
    [Test]
    public void Test()
    {
        var assembly = AssemblyLoadContext.Default.LoadFromAssemblyPath(@"C:\Path\To\Implementation.dll");
        var serviceType = assembly.GetExportedTypes().First(t => t.Name == "Service");
        Assert.That(serviceType.GetInterfaces().Single(), Is.EqualTo(typeof(IService)));
    }
}
  • Update the NUnit3TestAdapter package to 4.4.0
  • Build the solution.
  • Update the path to the built Implementation.dll
  • Run the test.

The test fails. On any version of NUnit3TestAdapter before 4.4.0 it passes.

The test solution is attached.

LoadAssemblyTest.zip

@OsirisTerje
Copy link
Member

@bhaeussermann Thanks! Appreciate the repro!
This adds to the #1065 and #1066 issues.

@OsirisTerje
Copy link
Member

@bhaeussermann Can you verify if the fix attached to #1066 (and same in #1065) works for your repro?

@bhaeussermann
Copy link
Author

@OsirisTerje It does! Thanks! 🎉

@OsirisTerje
Copy link
Member

Hotfix version 4.4.2, nearly identical to the alpha version attached yesterday, is now released. The difference from the attached version is that the Microsoft.Extensions.DependencyModel is not included in the adapter package. It is not needed there, so it was removed. The release notes is here https://docs.nunit.org/articles/vs-test-adapter/AdapterV4-Release-Notes.html (includes some more explanations) and the package is uploaded to nuget https://www.nuget.org/packages/NUnit3TestAdapter/4.4.2. Thanks, everyone for reporting and providing repros!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants