Hi, I'm still in the learning phase of Unit Testing in Unity and after 2 weeks of struggling I managed to code my test that work for my game project and I don't seem to understand why I'm getting these error messages. Can someone please assist me?
So these are my test codes:
using System.Collections;
using System.Collections.Generic;
using NUnit.Framework;
using UnityEngine;
using UnityEngine.TestTools;
[TestFixture]
public class TestSuite{
private GameObject testObject;
private HookMovement hookMovement;
[SetUp]
public void Setup()
{
testObject = GameObject.Instantiate(new GameObject());
}
[UnityTest]
public IEnumerator Verifying_For_All_The_Active_Items_In_The_Scene()
{
UnityEngine.SceneManagement.SceneManager.LoadScene("Gameplay");
yield return new WaitForSeconds(5);
//Arrange
var blueGem = GameObject.FindGameObjectWithTag("BlueGem");
var pinkGem = GameObject.FindGameObjectWithTag("PinkGem");
var lightBlueGem = GameObject.FindGameObjectWithTag("LightBlueGem");
var purpleGem = GameObject.FindGameObjectWithTag("PurpleGem");
var greenGem = GameObject.FindGameObjectWithTag("GreenGem");
var orangeGem = GameObject.FindGameObjectWithTag("OrangeGem");
var cashBag = GameObject.FindGameObjectWithTag("CashBag");
var largeStone = GameObject.FindGameObjectWithTag("LargeStone");
var smallStone = GameObject.FindGameObjectWithTag("SmallStone");
//Act
if (blueGem != null) Debug.Log("GameObject: " + blueGem.tag + " is active");
if (pinkGem != null) Debug.Log("GameObject: " + pinkGem.tag + " is active");
if (lightBlueGem != null) Debug.Log("GameObject: " + lightBlueGem.tag + " is active");
if (purpleGem != null) Debug.Log("GameObject: " + purpleGem.tag + " is active");
if (greenGem != null) Debug.Log("GameObject: " + greenGem.tag + " is active");
if (orangeGem != null) Debug.Log("GameObject: " + orangeGem.tag + " is active");
if (cashBag != null) Debug.Log("GameObject: " + cashBag.tag + " is active");
if (largeStone != null) Debug.Log("GameObject: " + largeStone.tag + " is active");
if (smallStone != null) Debug.Log("GameObject: " + smallStone.tag + " is active");
//Assert
Assert.AreEqual("BlueGem", blueGem.tag);
Assert.AreEqual("PinkGem", pinkGem.tag);
Assert.AreEqual("LightBlueGem", lightBlueGem.tag);
Assert.AreEqual("PurpleGem", purpleGem.tag);
Assert.AreEqual("GreenGem", greenGem.tag);
Assert.AreEqual("OrangeGem", orangeGem.tag);
Assert.AreEqual("CashBag", cashBag.tag);
Assert.AreEqual("LargeStone", largeStone.tag);
Assert.AreEqual("SmallStone", smallStone.tag);
yield return new WaitForSeconds(5);
}
[UnityTest]
public IEnumerator Verifying_If_The_Hook_Stops_Swinging_When_Triggered()
{
yield return new WaitForSeconds(1);
hookMovement = testObject.AddComponent();
if (hookMovement.canRotate = false) Debug.Log("The hook has stopped swinging");
Assert.IsFalse(hookMovement.canRotate);
}
[UnityTest]
public IEnumerator Verifying_If_The_Hook_Drops_When_Triggered()
{
yield return new WaitForSeconds(1);
hookMovement = testObject.AddComponent();
if (hookMovement.moveDown = true) Debug.Log("The hook has moved down");
Assert.IsTrue(hookMovement.moveDown);
}
}
So here's the thing, the game plays during the testing and I am able to trigger the hook to move down, so both my unit test for (Verifying_If_The_Hook_Stops_Swinging_When_Triggered) and (Verifying_If_The_Hook_Drops_When_Triggered) passes without the need for me to simulate a mouse click in the test code. However, (Verifying_For_All_The_Active_Items_In_The_Scene) fails and gives a error "System.NullReferenceException : Object reference not set to an instance of an object"
I also get this error "MissingReferenceException: The object of type 'GameplayManager' has been destroyed but you are still trying to access it.
Your script should either check if it is null or you should not destroy the object.
GameplayManager.DisplayScore (System.Int32 scoreValue) (at Assets/Scripts/Helper Scripts/GameplayManager.cs:87) Item.OnDisable () (at Assets/Scripts/Item Scripts/Item.cs:19)"
This is the code on line 87 in the Gameplay Manager Script:
StopCoroutine("Countdown");
This is the code on line 19 in the Item Script:
GameplayManager.instance.DisplayScore(scoreValue);
↧