15

Let's say my solution has 2 projects:

  • The first called "MainProject" (A .NETStandard 2.0 project).
  • The second called "MainProjectTests" (A NUnit test project) with some unit tests for each class into "MainProject".

The first project (MainProject) has a NuGet dependency called "dependencyX". Obviously, the project "MainProjectTests" has a reference to "MainProject".

So when the test runner runs a test of "MainProjectTests" that calls methods from "MainProject" using "dependencyX" I'm getting a System.IO.FileNotFoundException exception:

System.IO.FileNotFoundException : Could not load file or assembly 'dependencyX, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies.

Why am I getting this exception? When I add "dependencyX" to "MainProjectTests" all works fine, but it seems to me not a good practice... How to solve it?

I'm using Visual Studio for Mac Community 7.2 preview (7.2 build 583)

Thanks for the help.

EDIT:

Tried putting the options:

<RestoreProjectStyle>PackageReference</RestoreProjectStyle>

<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>

in the NUnit project, but getting the same result.

Bruno Peres
  • 15,845
  • 5
  • 53
  • 89
  • 1
    Why doesn't it seem like a good practice to have dependencies available where you use them? Where should the runtime get them from instead? – CodeCaster Sep 14 '17 at 14:03
  • 2
    https://stackoverflow.com/questions/44706516/importing-nuget-references-through-a-local-project-reference – CodeCaster Sep 14 '17 at 14:03
  • @CodeCaster thanks for the comment. My test project is not directly using `dependencyX`. Instead, it is calling a method from a class inside a project that uses `dependencyX`. I am wrong? So I believe the test project should not know about the existence of `dependencyX`... – Bruno Peres Sep 14 '17 at 14:14
  • 1
    Is MainProject set up to put the dependencyX binaries in the build output directory? If not, why would you expect it to be runnable? – Zastai Sep 16 '17 at 12:07
  • @Zastai In this do I need to configure all my dependencies individually to be copied to the output directory? – Bruno Peres Sep 19 '17 at 11:16
  • It is *supposed* to be possible, NUnit uses the AssemblyResolve event to help the CLR find files in unusual places. But they've been struggling to make that work well for .NETCore, see [this issue](https://github.com/nunit/nunit/issues/2237) for example. They did something about it just 27 days ago. An update you might not yet have or do have and it is buggy for v2.0, hard to guess. Nobody ever likes debugging their unit test runner, just click the New Issue button. – Hans Passant Sep 19 '17 at 13:02
  • Even if it's an NUnit bug, copying dependencies might still be a workaround. Set dependencyX as copy to output and build MainProject. Set MainProject as copy to output for the test project and build that. If that results in the dependencyX assemblies appearing in the test project's bin folder, I would expect running to work. You'd have to do that for all runtime dependencies of MainProject. Note that if you're using the automatic nuget packaging for MainProject, you probably don't want to do this as it might place dependencyX' binaries in your nupkg too. – Zastai Sep 19 '17 at 15:49
  • @Zastai can you point me how to check if I'm using automatic nuget packaging for MainProject in Visual Studio for Mac? Thanks for your comments. – Bruno Peres Sep 20 '17 at 11:23
  • @BrunoPeres When you create a nupkg for MainProject as part of the build and you don't use a nuspec you maintain yourself. – Zastai Sep 20 '17 at 11:54
  • 1
    Copying the dll in the test bin works but it's a weird workaround. – flchaux Nov 07 '17 at 10:59
  • @flchaux I agree with you! – Bruno Peres Nov 07 '17 at 11:02

2 Answers2

6

This seems to be a known bug regarding .NET Standard libraries (and maybe especially in conjunction with NUnit). I already filed a bug report here which seems to confirm that this is not the intended behaviour. Altough there has been no progress for over half a year.

Maybe one should file a bug in the NUnit repo after confirming this only happens when using NUnit.

For the time beeing you'll need to reference all libraries used in a .NET Standard project also in all projects referencing the .net standard one as you are doing right now.

CShark
  • 1,413
  • 15
  • 25
1

It is a bug reported to Microsoft few times and it seems they did not do much on this, look at this Visual Studio does not copy referenced assemblies through the reference hierarchy

On the other hand at least with Nuget Packages you have a simple way (to add the same package to multiple projects in the same solution) using the package manager for the solution, as you can see here Nuget Package Manager.

solution manager

enter image description here

Victor Hugo Terceros
  • 2,969
  • 3
  • 18
  • 31