Topic: destroying instances during unit and integration tests

Is there an easy way to destroy all the various instances to ensure each test starts with a clean state? At first glance, looks like we use the instance properties of these classes:

OnlineMaps
OnlneMapsControlBase3D
OnlineMapsTileSetControl

Thanks,
Tim

Re: destroying instances during unit and integration tests

Hello.

Just destroy the map GameObject, and then create a new instance from the prefab.

Kind Regards,
Infinity Code Team

3 (edited by movestill 2018-07-30 18:46:27)

Re: destroying instances during unit and integration tests

Thanks for the prompt reply. Any idea why Awake() isn't called after instantiating the prefab? In the prefab, the GameObject is active as is its OnlineMaps and OnlineMapsTileSetControl components.

Actually, after looking further, it seems like the instantiated prefab can't be activated for some reason.

Here's bare minimum test code using Unity 2017.3 and OnlineMaps 2.5.30.1:

    [TestFixture]
    public class MapTest
    {
        [UnityTest]
        public IEnumerator TestMap()
        {
            var mapObj = Resources.Load("Prefabs/UI/Map") as GameObject;
            Assert.IsNotNull(mapObj);
            yield return null;
            mapObj.SetActive(true);
            yield return null;

// This assert fails.

            Assert.True(mapObj.activeInHierarchy);
        }
    }

Thanks,
Tim

Re: destroying instances during unit and integration tests

Here is the explanation:
https://answers.unity.com/questions/698 … nit-t.html

Unfortunately, I'm not a big helper in unit testing, because I do not use it in Unity.

Kind Regards,
Infinity Code Team

Re: destroying instances during unit and integration tests

Thanks for taking a look, Alex. Turns out that my problem was not instantiating the map GameObject after loading it as a resource. I've been using Zenject for loading prefabs and forgot how to do it the standard way. For posterity, here's how to load the map as part of a Unity play mode test:

        [UnityTest]
        public IEnumerator TestMapLoaded()
        {
            var mapObj = Resources.Load("Prefabs/UI/Map") as GameObject;
            Assert.IsNotNull(mapObj);

            var mapInstance = GameObject.Instantiate(mapObj);
            var camera = mapInstance.AddComponent<Camera>();
            Assert.True(mapInstance.activeInHierarchy);
            var maps = mapInstance.GetComponent<OnlineMaps>();
            Assert.IsNotNull(maps);
            var tileset = mapInstance.GetComponent<OnlineMapsTileSetControl>();
            Assert.IsNotNull(tileset);
            tileset.activeCamera = camera;
            Assert.IsNotNull(tileset);
            Assert.True(maps.isActiveAndEnabled);
            Assert.IsNotNull(OnlineMaps.instance);
            Assert.IsNotNull(tileset.activeCamera);
            yield return null;
        }

Tim