tpierrain/NFluent

Could not load file or assembly Microsoft.VisualStudio.TestPlatform.ObjectModel when assertion fails

Closed this issue · 3 comments

I have generated a new xunit test project using .net core 2.0, then I have changed the target framework to:

  <PropertyGroup>
    <TargetFramework>net461</TargetFramework>
  </PropertyGroup>

By default the following libraries are referenced and I could restore them after changing target framework (there is also NFluent that I referenced myself):

  <ItemGroup>
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.8.0" />
    <PackageReference Include="NFluent" Version="2.3.1" />
    <PackageReference Include="xunit" Version="2.4.0" />
    <PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
  </ItemGroup>

When I run tests using command line dotnet test using the following test class:

    public class UnitTest1
    {
        [Fact]
        public void Test1()
        {
            Check
                .That(new Exception())
                .IsSameReferenceAs(new Exception());
        }
    }

I get the following error:

[xUnit.net 00:00:01.86]     CyberVadis.Core.Functions.Test.UnitTest1.Test1 [FAIL]
Failed   CyberVadis.Core.Functions.Test.UnitTest1.Test1
Error Message:
 System.IO.FileNotFoundException : Could not load file or assembly 'Microsoft.VisualStudio.TestPlatform.ObjectModel, Version=11.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.
Stack Trace:
   at System.Reflection.RuntimeAssembly.GetExportedTypes(RuntimeAssembly assembly, ObjectHandleOnStack retTypes)
   at System.Reflection.RuntimeAssembly.GetExportedTypes()
   at NFluent.Helpers.ExceptionHelper.ExceptionScanner(String assemblyMarker, String nameSpace, String assertionExceptionName, String ignoreExceptionName, String inconclusiveExceptionName)
   at NFluent.Helpers.ExceptionHelper.get_Constructors()
   at NFluent.Helpers.ExceptionHelper.BuildException(String theMessage)
   at NFluent.Kernel.ExceptionReporter.ReportError(String message)
   at NFluent.Kernel.CheckLogic`1.ReportError(FluentMessage fluentMessage)
   at NFluent.Kernel.CheckLogic`1.EndCheck()
   at NFluent.ObjectCheckExtensions.IsSameReferenceAs[T,TU](ICheck`1 check, TU expected)
   at CyberVadis.Core.Functions.Test.UnitTest1.Test1() in C:\Users\xxxx\Class1.cs:line 16

Temporary workaround is to add the following package:

<PackageReference Include="Microsoft.TestPlatform.ObjectModel" Version="15.9.0" />

I believe that problem is related here to too eager scan of the referenced assemblies assemblies. E.g. the line

var resultScan = ExceptionScanner("visualstudio", "Microsoft.VisualStudio.TestTools", "AssertFailedException", null, "AssertInconclusiveException")
will also will try to load and scan xunit.runner.visualstudio assembly which is unnecessary.

Thanks for your detailed report. I was able to reproduce this issue with dotnet test and with visual studio test runner.
Oddly, NCrunch hides this issue. I am still analysing.

Hello
can you test the beta version on MyGet and confirm this fixes your issue?
https://www.myget.org/feed/dupdobnightly/package/nuget/NFluent

I confirm - everything works correctly after fix. Thanks!