Installed Unity Test runner but can't access NSubstitute

All the resources on how to use unity’s test runner talk about using NSubstitute to mock objects, which sounds great, but not a single one explains how to actually get NSubstitute working with unity. It isn’t included with the test runner (using 2017.2) (although NUnit is). Installing it to visual studio using NuGet doesn’t work because the dependencies are all messed up.

Is something wrong with my unity install? This is driving me insane!

I believe it was removed from the Testing tools when they were merged with Unity.

I was looking into this as well, download the lastest from the link above.
Add it in your Editor folder as Import new Asset…
pull the dll from the NET35 folder

I’m on windows and had to use visual studio, as vs code did not recognize NSubstitute for some reason.

Also checkout this:

For anyone seeing this in the future, for Unity 2018 you’ll need Version 2 of the NSubstitue Nuget Package

I’m actually having a similar problem.

I imported the NSubstitute.dll i found inside the net35 folder of the package, into the Unity project, but Unity does not recognize it.

Importing the NSubstitute package directly from Visual Studio, allow the IDE to recognize it but only as soon as I jump again into the Unity Editor, the code recompiles and the NSubstitute package is not recognized anymore giving me compilation errors (for using Substitute).

I tried with both .NET Standard 2.0 and .NET 4.x configurations for the Api Compatibility Level on the Player Tab under the Project settings.

Thanks in advance for any reply :slight_smile:


OS: Windows 10 (64bit)

Version of Unity used: 2019.2.3f1.

Versions of NSubstitute tried: 4.2.1 / 4.0.0 / 2.0.3.


UPDATE: problem solved.

I worked a bit on it and I managed to find the solution to the issue.
I discovered that the NSubstitute .dll file that I added to the project is not ‘automatically’ visible to the test code.

In order to make it so, it has been enough to reference the NSubstitute .dll file from the assembly definition in the test folder, under the Assembly Reference section - where the reference to NUnit dll is -.

This was the missing piece for me. I hope this note will be helpful to others that might reach this post as well.

It’s also possible to get the latest version 4 of NSubstitute working by adding NSubstitute’s dependency DLLs to the Plugins folder.


See this Stack Overflow answer.

I had to add these DLLs:

  • NSubstitute.dll
  • System.Threading.Tasks.Extensions.dll
  • Castle.Core.dll

Note that I tested with netstandard 2.0 DLL files with “.NET 4.x” API Compatibility Level project setting on Unity 2020.


I found it easiest to get all the required DLLs by installing NSubstitute via the NuGet package manager which pulls in the dependencies.