Skip to main content
6 of 6
edited body
user3797758
  • 3.7k
  • 2
  • 31
  • 53

How to make an Assert.IsNull test pass when the value is reported as <null>?

I've just started using unity tests with the built in editor tests. After making my test and finding out that it constantly failed I debugged the code and found that a static object was set to a new game object when running the test while it is set to null when playing in the editor.

Is there a way to force unity to treat the test the in the same way that it treats scripts at run time?

Edit

After changing some code around i get another strange error: Editor Tests window showing error

does anyone know why "null" isn't "null". The null objects are stored in an array, but that shouldn't make a difference right?

Edit Edit (Edit 2?)

Here is the code that has the issue

public class StatCollector : MonoBehaviour { public static StatCollector running; //ensure that there is only one object running at the same time public void Awake () { if(running == null) { DontDestroyOnLoad(gameObject); running = this; } else if(running != this) { DestroyImmediate(gameObject); } } } 

and here is the test:

[Test] public void SingleTest() { //create gameobjects GameObject[] collect = new GameObject[2]; collect[0] = new GameObject("one"); collect[1] = new GameObject("two"); collect[0].AddComponent<StatCollector>(); //see if gameobjects were created correctly if (collect[0] == null || collect[1] == null) Assert.Fail("failed to create game objects"); //test what happens when the second stat collector is added to the scene collect[1].AddComponent<StatCollector>(); collect[0].GetComponent<StatCollector>().Awake(); collect[1].GetComponent<StatCollector>().Awake(); Assert.IsNull(collect[1]); } 

I'm assuming that there is no way to keep Awake private which is why it is public (just so that the test case has access to it)

user3797758
  • 3.7k
  • 2
  • 31
  • 53