Make a GameObject
We can create a
GameObject inside a test just like we would normally inside a Unity script.
Passing a string to the
GameObject constructor gives the
GameObject that name.
We can even Instantiate prefabs
Since our test is not inheriting
MonoBehaviour, we’ll have to call the Instantiate function directly, using the fully qualified namespace.
Remember that prefabs are just stored
GameObjects, so we can use the
testGo created above as our prefab.
1 2 3
We can create
GameObjects, but repeatedly running the test leaves our project cluttered up with old objects. We want our tests to clean up all mocked objects after running. We’ll do that using by using the fully qualified
We need to call
DestroyImmediate instead of
Destroy because the unit tests are running in the editor and delayed destruction won’t be invoked.
Mocking is easy
Following this pattern will give you the ability to mock objects to your hearts’ delight as you unit test your Unity code. As, always let me know if you have questions, comments, or concerns below or on twitter.