- Home /
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!
Answer by Kyle-h · Oct 26, 2017 at 12:13 AM
I believe it was removed from the Testing tools when they were merged with Unity.
https://github.com/nsubstitute/NSubstitute/releases
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:
Answer by Eli-Davis · Aug 05, 2019 at 02:04 PM
For anyone seeing this in the future, for Unity 2018 you'll need Version 2 of the NSubstitue Nuget Package
This is the right answer! Get it here https://www.nuget.org/packages/NSubstitute/2.0.3. I tried getting 4.2.1 and even got the assembly dependencies (Castle.Core, System.Threading.Tasks.Extensions and System.Runtime.CompilerServices.Unsafe), but that did not work: Unity kept logging an error saying "Unloaded broken assembly ...".
Answer by Amheklerior · Sep 28, 2019 at 11:06 AM
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 :)
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.
Answer by sonnyb · May 26, 2021 at 03:56 PM
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.
You're a lifesaver - Unity was complaining about NSubstitute but only in one assembly and only on a single machine at our studio. Adding Castle.Core to the assembly resolved the issue.
Your answer
Follow this Question
Related Questions
iOS development on PC 1 Answer
Testing for Active Internet Connection 4 Answers
Unit Testing Rigidbody2D without polluting scene with gameobjects 1 Answer
Unit Testing MonoBehaviour.Assertion component not Working with property. 2 Answers
Android game testing, using Unity Remote 4 NOT working 1 Answer