Search Unity

  1. Are you interested in providing feedback directly to Unity teams? Sign up to become a member of Unity Pulse, our new product feedback and research community.
    Dismiss Notice

Help Wanted Unity project can't resolve Moq dependency

Discussion in 'Testing & Automation' started by obscure-sphinx, Oct 2, 2020.

  1. obscure-sphinx

    obscure-sphinx

    Joined:
    Oct 1, 2020
    Posts:
    3
    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

    Assets/Tests/TestScript.cs(3,7): error CS0246: The type or namespace name 'Moq' could
    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
     
  2. Warnecke

    Warnecke

    Unity Technologies

    Joined:
    Nov 28, 2017
    Posts:
    71
    Hey. Have you remembered to add your Moq.dll to the assembly references for your test script?
     
    firepad9 likes this.
  3. obscure-sphinx

    obscure-sphinx

    Joined:
    Oct 1, 2020
    Posts:
    3
    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?
     
  4. Warnecke

    Warnecke

    Unity Technologies

    Joined:
    Nov 28, 2017
    Posts:
    71
    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",
     
    obscure-sphinx likes this.
  5. obscure-sphinx

    obscure-sphinx

    Joined:
    Oct 1, 2020
    Posts:
    3
    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
     
unityunity