Search Unity

  1. Check out the Unite LA keynote for updates on the Visual Effect Editor, the FPS Sample, ECS, Unity for Film and more! Watch it now!
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  4. Want more efficiency in your development work? Sign up to receive weekly tech and creative know-how from Unity experts.
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  6. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Testing and NetworkBehaviours

Discussion in 'Connected Games' started by Vexorum, Sep 16, 2018.

  1. Vexorum


    Jan 9, 2016

    I've been trying all day to set up some automated tests with the Unity Test Runner for some various units in my game. Here's an example of what I have.

    Code (CSharp):
    1.  [UnityTest]
    2.         public IEnumerator MinerShouldMineMineral()
    3.         {
    4.             yield return new WaitForSeconds(3.1f);
    6. // Needed for unit admin
    7.             _autoPlayerNodeGo = new GameObject();
    8.    = "AutoPlayerNode";
    10.             yield return new WaitForSeconds(3.1f);
    12. // A chunk of iron ore. Place it on the map.
    13.             var orePrefab = Resources.Load(@"Prefabs\Minerals\IronOre");
    14.             var ore = Instantiate(orePrefab, EndPosition, Quaternion.identity) as GameObject;
    16.             yield return new WaitForSeconds(3.1f);
    18. // A resource mining unit. Place it on the map.
    19.             var prefab = Resources.Load(@"Prefabs\Units\Miner");
    20.             _miner = Instantiate(prefab, StartPosition, Quaternion.identity) as GameObject;
    21.             _miner.GetComponent<Unit>().PlayerInfo = autoplayerNode;
    23.             yield return new WaitForSeconds(3.1f);
    25. // Code omitted: Order the mining unit to pick up the iron ore.
    27.             yield return new WaitForSeconds(20.0f);
    29. // Assert, the miner has picked up the ore.
    30.             var inventory = _miner.GetComponent<IInventoryBlock>();
    31.             Assert.That(inventory.GetUsedCapacity(), Is.EqualTo(1));
    32.         }
    I feel like I'm almost there. But here's the problem that has me blocked. The miner unit and the ore block use a few CMD methods with the [Command] attribute to sync with the server.

    Code (CSharp):
    2.         [Command]
    3.         public void CmdSetBeingMined()
    4.         {
    5.             IsBeingMined = true;
    6.             _rb.isKinematic = true;
    7.             _rb.detectCollisions = false;
    8.         }
    10.         [Command]
    11.         public void CmdClaimResource(string claimerId)
    12.         {
    13.             if (isServer)
    14.             {
    15.                 ClaimerId = claimerId;
    16.             }
    17.         }
    This runs wonderfully when I play in the editor. Or when I build and run a client and join a few instances together. But in the Test Runner it fails and I get this:

    Command function CmdClaimResource called on server.

    I'm using the test runner in Play mode. Everything is behaving as expected. But I can't seem to get the Cmd methods to execute the same way they do when I run the game in the editor and play as host.

    It's beginning to feel like I'm fighting the framework. So I suspect I'm trying to build my tests wrong. Any insight on how to approach testing of network behaviours would be very much appreciated.
    Last edited: Sep 16, 2018