Unit Testing MonoBehaviour.Assertion component not Working with property.

Everyone knows that you can’t use new with MonoBehaviour.
What I am doing is put everything that doesn’t use the unity API inside another class then reference it. This left me with MonoBehaviours that uses only the other class. Is this the correct way to go.

Update

This time I had a problem with the integration tests. I have a code that goes like this.

    class Foo : MonoBehavior{
    
    public GameObject AGameObject{
       get{ return bar.AGameObject}
       set{ bar.AGameObject = value}
       
    AnotherClass bar;

    void Start(){
       bar = new AnotherClass ();
    }
}

I get when I use the Assertion Component:

Unexpected top level layout group! Missing GUILayout.EndScrollView/EndVertical/EndHorizontal?
UnityEditor.DockArea:OnGUI()

Any Help ?

Hey!

Your approach is fine. There is nothing wrong with decoupling game logic from Unity API :slight_smile:
I’ve written a blog post about it, check it out: Unit testing part 2 - Unit testing MonoBehaviours | Unity Blog

Tomek

For the update I was able to solve the problem. I have to test for null value.

class Foo : MonoBehavior{
     
     public GameObject AGameObject{
        get
        { 
             If(bar == null)
                return null;
             return bar.AGameObject;
        }
        set
        { 
             If(bar == null)
                bar = new AnotherClass ();
             bar.AGameObject = value;
        }
        
     AnotherClass bar;
 
     void Start(){
        if(bar == null)
            bar = new AnotherClass ();
     }
 }