Unity project can't resolve Moq dependency

Just some background, most of my programming knowledge lies with Java and Python. I did one semester of C# coding in my undergrad CS degree, and that was more focused on web development, so for the purposes of developing with Unity, I’m pretty much a neophyte. I’m planning on coding using Jetbrains Rider as an external editor, and I’m using Unity 2019.4.

So I made my project, and added a few scripts under Assets/Scripts. I opened the Test Runner, and made an Edit Mode test assembly folder at Assets/Tests for some good old fashioned TDD. At this point, Unity gave me a warning along the lines of some test assembly that’s not going to be compiled, because there aren’t any scripts associated with it. I ran the tests from the Unity Test Runner anyway, and they passed, so I didn’t worry too much about it. I opened up the generated test file, and ran the tests from within Rider, and the two generated tests passed here as well, so far so good.

I wanted to use the Moq library for mocking out some of my interfaces, so I download Moq using NuGet from Rider, and added it to my testing project. Those DLLs ended up in the packages folder in my project root, with Moq 4.14.6, and three other packages that are probably dependencies for Moq itself.

I generated an assembly definition in my scripts folder and called it GameAssembly, then pointed my test assembly’s definition references to it via Unity. Then I added a new test in my dummy test script mocking out an interface and doing a simple assertion, and Rider pulled in and resolved all the Moq related stuff I was using, and neither Rider nor Unity was complaining about anything at this point. I tried running all the tests from Rider, and then things started breaking. Next thing, I’ve got errors on all my Moq related code. Unity also can’t seem to resolve Moq at all, with a red error from the Unity editor saying

not be found (are you missing a using directive or an assembly reference?)```

and basically the same error coming from Rider as well, saying "cannot resolve symbol 'Moq'." There is obviously a using directive in my code for Moq, so the error probably lies with the assembly reference, but I have no idea where to begin fixing it. What's strange to me is that it seemed to work fine up until I actually executed the tests. Any help would be greatly appreciated

Hey. Have you remembered to add your Moq.dll to the assembly references for your test script?

1 Like

Right now the test asmdef only has an assembly reference to nunit’s dll. When I try to add this reference via Inspector along with the nunit dll, it just says “no possible references.” I’m thinking that it might be because the dll is under the packages folder from my project root. How do I point to Moq.dll in that folder from my asmdef?

If you try to move your dll inside your assets folder, then it should show up in the dropdown.
Alternatively you can change your package.json manifest to reference the moq unity package: “nuget.moq”: “1.0.0”,

2 Likes

Okay, it worked when I added “nuget.moq”: “1.0.0” to the manifest file! I added the dll to the asmdef via Inspector, and the dummy test compiles and runs fine now. Thanks a lot for your help