Search Unity

  1. Unity 2019.1 beta is now available.
    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. We're looking for insight from anyone who has experience with game testing to help us better Unity. Take our survey here. If chosen to participate you'll be entered into a sweepstake to win an Amazon gift card.
    Dismiss Notice
  4. On February 28th the Feedback website will shut down and be redirected to the Unity forums. See the full post for more information.
    Dismiss Notice
  5. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  6. Unity 2018.3 is now released.
    Dismiss Notice
  7. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    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