tpierrain / NFluent

Smooth your .NET TDD experience with NFluent! NFluent is an ergonomic assertion library which aims to fluent your .NET TDD experience (based on simple Check.That() assertion statements). NFluent aims your tests to be fluent to write (with a super-duper-happy 'dot' auto-completion experience), fluent to read (i.e. as close as possible to plain English expression), but also fluent to troubleshoot, in a less-error-prone way comparing to the classical .NET test frameworks. NFluent is also directly inspired by the awesome Java FEST Fluent assertion/reflection library (http://fest.easytesting.org/)
Apache License 2.0
310 stars 53 forks source link

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

Closed marcin-chwedczuk-meow closed 6 years ago

marcin-chwedczuk-meow commented 6 years ago

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 https://github.com/tpierrain/NFluent/blob/e63413aca0c823380360e70604fca65e2e6c39ce/src/NFluent/Helpers/ExceptionHelper.cs#L58 will also will try to load and scan xunit.runner.visualstudio assembly which is unnecessary.

dupdob commented 6 years ago

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.

dupdob commented 6 years ago

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

marcin-chwedczuk-meow commented 6 years ago

I confirm - everything works correctly after fix. Thanks!