Nsubstitute: Could not load file or assembly NSubstitute

Created on 17 Oct 2017  路  7Comments  路  Source: nsubstitute/NSubstitute

Hey,

Firstly, I'm not sure if it is the right place for my problem, but I will give it a try.

I might be missing something but I am not able to spin up .NET Core 2.0.0 test project using xunit and NSubstitute. I'm getting:

System.IO.FileNotFoundException : Could not load file or assembly 'NSubstitute, Version=3.0.1.0, Culture=neutral, PublicKeyToken=92dd2e9066daa5ca'. The system cannot find the file specified.

Steps to reproduce:

  1. Create empty .NET Core class library with dotnet new classlib
  2. Target netcoreapp2.0 framework

    <TargetFramework>netcoreapp2.0</TargetFramework>

  3. Add xunit package reference
    dotnet add package xunit --version 2.3.0

  4. Add dotnet CLI tool reference

    <DotNetCliToolReference Include="dotnet-xunit" Version="2.3.0" />

  5. Add NSubtitute package reference
    dotnet add package NSubstitute --version 3.0.1

  6. Add simple test with simple Substitute functionality

public interface IService
{
    void Method();
}

public class Class1
{
    [Fact]
    public void Test1()
    {
        var mockedService = Substitute.For<IService>();
    }
}
  1. Restore, Build and Run project.
    dotnet restore
    dotnet build
    dotnet xunit

  2. Observe output

Running .NET Core 2.0.0 tests for framework netcoreapp2.0...
xUnit.net Console Runner (64-bit .NET Core 4.6.00001.0)
  Discovering: testxunit
  Discovered:  testxunit
  Starting:    testxunit
    testxunit.Class1.Test1 [FAIL]
      System.IO.FileNotFoundException : Could not load file or assembly 'NSubstitute, Version=3.0.1.0, Culture=neutral, PublicKeyToken=92dd2e9066daa5ca'. The system cannot find the file specified.

      ---- System.IO.FileNotFoundException : Could not load file or assembly 'NSubstitute, Version=3.0.1.0, Culture=neutral, PublicKeyToken=92dd2e9066daa5ca'. The system cannot find the file specified.

      Stack Trace:

        ----- Inner Stack Trace -----
           at System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks, IntPtr ptrLoadContextBinder)
           at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, IntPtr ptrLoadContextBinder)
           at System.Reflection.Assembly.Load(AssemblyName assemblyRef, IntPtr ptrLoadContextBinder)
           at System.Runtime.Loader.AssemblyLoadContext.LoadFromAssemblyName(AssemblyName assemblyName)
           at System.Runtime.Loader.AssemblyLoadContext.ResolveUsingLoad(AssemblyName assemblyName)
           at System.Runtime.Loader.AssemblyLoadContext.Resolve(IntPtr gchManagedAssemblyLoadContext, AssemblyName assemblyName)
  Finished:    testxunit
=== TEST EXECUTION SUMMARY ===
   testxunit  Total: 1, Errors: 0, Failed: 1, Skipped: 0, Time: 0.173s

Here is test project .csproj file

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netcoreapp2.0</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="NSubstitute" Version="3.0.1" />
    <PackageReference Include="xunit" Version="2.3.0" />
  </ItemGroup>
  <ItemGroup>
    <DotNetCliToolReference Include="dotnet-xunit" Version="2.3.0" />
  </ItemGroup>
</Project>

I'm on Ubuntu 16.04 LTS 64bit

I would be grateful if you guys give me any hint on that. If it's wrong place then sorry for wasting your time. Thanks!

bug

Most helpful comment

@korzonkie Sorry for late reply, I didn't have time to work on it earlier. In short, it's an xunit bug, most probably introduced in 2.3.0 versions, similar issues: https://github.com/xunit/xunit/issues/1509 https://github.com/xunit/xunit/issues/1500

I've tried to update xunit to the latest 2.4.0-beta1-build3832 from their MyGet feed and it worked for me.
Here's my csproj:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netcoreapp2.0</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="xunit" Version="2.4.0-beta1-build3832" />
    <PackageReference Include="NSubstitute" Version="3.0.1" />
  </ItemGroup>
  <ItemGroup>
    <DotNetCliToolReference Include="dotnet-xunit" Version="2.4.0-beta1-build3832" />
  </ItemGroup>
</Project>

Also you need to add the MyGet feed. To add it you can use Visual Studio UI or NuGet config file. I put NuGet.config along the csproj with the following content:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <packageRestore>
    <add key="enabled" value="True" />
    <add key="automatic" value="True" />
  </packageRestore>
  <packageSources>
    <add key="nuget.org" value="https://www.nuget.org/api/v2/" />
    <add key="MyGet" value="https://www.myget.org/F/xunit/api/v3/index.json" />
  </packageSources>
  <disabledPackageSources />
  <activePackageSource>
    <add key="All" value="(Aggregate source)" />
  </activePackageSource>
</configuration>

I hope this helps.

All 7 comments

Hi @korzonkie,

Thank you for raising the issue. I can confirm that I can reproduce it on my Ubuntu 16.04 LTS 64bit (while it works on Windows). I have a gut feeling that it has something to do with assembly signing ( #326 https://github.com/nsubstitute/NSubstitute/commit/18f7580684df0b8a51cf33b6b76913ced5915dc2). I'll take a look at that tomorrow. Just want to note that issue is pretty important cc @dtchepak

Update: Unsigned version (3.0.0) doesn't work either. 馃槥

Can not reproduce this on OS X.

Does it work on Ubuntu with dotnet test (requires Microsoft.NET.Test.Sdk package reference stuff)?

@korzonkie Sorry for late reply, I didn't have time to work on it earlier. In short, it's an xunit bug, most probably introduced in 2.3.0 versions, similar issues: https://github.com/xunit/xunit/issues/1509 https://github.com/xunit/xunit/issues/1500

I've tried to update xunit to the latest 2.4.0-beta1-build3832 from their MyGet feed and it worked for me.
Here's my csproj:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netcoreapp2.0</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="xunit" Version="2.4.0-beta1-build3832" />
    <PackageReference Include="NSubstitute" Version="3.0.1" />
  </ItemGroup>
  <ItemGroup>
    <DotNetCliToolReference Include="dotnet-xunit" Version="2.4.0-beta1-build3832" />
  </ItemGroup>
</Project>

Also you need to add the MyGet feed. To add it you can use Visual Studio UI or NuGet config file. I put NuGet.config along the csproj with the following content:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <packageRestore>
    <add key="enabled" value="True" />
    <add key="automatic" value="True" />
  </packageRestore>
  <packageSources>
    <add key="nuget.org" value="https://www.nuget.org/api/v2/" />
    <add key="MyGet" value="https://www.myget.org/F/xunit/api/v3/index.json" />
  </packageSources>
  <disabledPackageSources />
  <activePackageSource>
    <add key="All" value="(Aggregate source)" />
  </activePackageSource>
</configuration>

I hope this helps.

Hey!

Works like a charm! Thank you very much for your involvement. I really appreciate it.

Thanks a lot for chasing this up @alexandrnikitin. I know you're very pressed for time so really appreciate you for spending some for us! 馃槂 馃憤

@korzonkie Great! I'm glad it helped.
@dtchepak Thank you. It's my duty and pleasure 馃槉

Was this page helpful?
0 / 5 - 0 ratings