Search Unity

Play mode tests public property initialization.

Discussion in 'Testing & Automation' started by Steedalion, Mar 5, 2021.

  1. Steedalion

    Steedalion

    Joined:
    Jul 6, 2020
    Posts:
    51
    I am new to unity testing and am running into some issues.
    I am testing but find that I have to initialize the properties of each component (monobehaviour). Why is that? enableOnZero;

    Consider this. An Placement task has a delegate that occurs on correct execution. A countdown subscriber observes the placement task and decrements accordingly. Once the timer hits zero it executes the EnableDisableBehavior which enables gameobjects. The designer specifies these object. How do I test this since the designer only specifies it in the editor?


    Code (CSharp):
    1. public class EnableOnComplete : MonoBehaviour
    2.     {
    3.         [SerializeField] public OnComplete subject;
    4.         public GameObject[] enableOnZero;
    5.         public GameObject[] disableOnZero;
    6.  
    7.  
    8.         public void EnableAndDisable()
    9.         {
    10.             foreach (GameObject obj in enableOnZero)
    11.             {
    12.          
    13.                 obj.SetActive(true);
    14.             }
    15.             foreach (GameObject obj in disableOnZero)
    16.             {
    17.          
    18.                 obj.SetActive(false);
    19.             }
    20.         }
    21.     }
    Code (CSharp):
    1. [UnityTest]
    2.         public IEnumerator EnableOnCompleteTestWithEnumeratorPasses()
    3.         {
    4.             yield return TestSetup();
    5.  
    6.             countdownOnComplete.counter = 2;
    7.  
    8.             _enableOnComplete.enableOnZero = new GameObject[1];
    9.             _enableOnComplete.enableOnZero[0] = enableCompleted;
    10.             // _enableOnComplete.disableOnZero = new GameObject[0];
    11.             enableCompleted.SetActive(false);
    12.             yield return null;
    13.  
    14.             Assert.IsFalse(enableCompleted.activeInHierarchy);
    15.  
    16.             placementTask.onCorrectCompletion.Invoke();
    17.             yield return null; //1
    18.             Assert.IsFalse(enableCompleted.activeInHierarchy);
    19.  
    20.             placementTask.onCorrectCompletion.Invoke();
    21.             yield return null; //0
    22.             Assert.IsTrue(enableCompleted.activeInHierarchy);
    23.  
    24.             placementTask.onCorrectCompletion.Invoke();
    25.             yield return null; //unchanged
    26.             Assert.IsTrue(enableCompleted.activeInHierarchy);
    27.  
    28.             yield return null;
    29.         }
    1. Why do I have to initialize disableOnZero? I thought since I am using play mode tests the editor would do that for me?
    2. How can I make this private serializefield and still insert and gameobject to enable and disable in order to test it?
    3. Is there something I missing in the steps. Create gameobject -> attach component -> attach testable hypothetical object -> predict outcome?
     
    Last edited: Mar 10, 2021
  2. Steedalion

    Steedalion

    Joined:
    Jul 6, 2020
    Posts:
    51
    Last edited: Mar 10, 2021