- Home /
PlayMode Monobehavior test
Hello,
When I am trying to run Monobehaviour test the test cannot return any objects via GetComponentsinChildren() method. It seems that it can only getComponent on the object where the tested script is pinned. Can you help me out what I am doing wrong?
Some example code:
Unity test code here:
[UnityTest]
public IEnumerator TestEnumeratorPasses()
{
yield return new MonoBehaviourTest<TestedScript>();
}
Tested class script here:
public class TestedScript : MonoBehaviour, IMonoBehaviourTest
{
public bool IsTestFinished
{
get
{
if (GetComponentsInChildren<SpriteRenderer>().Length != 3)
{
return false;
}
return true;
}
}
}
The problem is that the GetComponentsInChildren().Length returns always 0 in the test and normally in play mode it returns normal number of child Components that I am looking for.
Any idea how to accomplish this?
Answer by Warnecke · Jan 29, 2018 at 01:26 PM
Hey
The MonobehaviorTest can be used to test a mono behavior in isolation. It just creates a new game object and then adds the given component to it to the object. So in your case the object will not have any children or any SpriteRenderers.
I would suggest making a playmode test where you create the game objects in the way you want them your self.