docs.unity.cn
    Show / Hide Table of Contents

    Class MonoBehaviourTest<T>

    This is a wrapper that allows running tests on MonoBehaviour scripts. Inherits from CustomYieldInstruction.

    Inheritance
    Object
    CustomYieldInstruction
    MonoBehaviourTest<T>
    Inherited Members
    UnityEngine.CustomYieldInstruction.MoveNext()
    UnityEngine.CustomYieldInstruction.Reset()
    UnityEngine.CustomYieldInstruction.Current
    Namespace: UnityEngine.TestTools
    Syntax
    public class MonoBehaviourTest<T> : CustomYieldInstruction, IEnumerator where T : MonoBehaviour, IMonoBehaviourTest
    Type Parameters
    Name Description
    T

    A MonoBehaviour component created for the test and attached to the tests GameObject.

    Constructors

    MonoBehaviourTest(Boolean)

    MonoBehaviourTest is a coroutine and a helper for writing MonoBehaviour tests. Yield a MonoBehaviourTest when using the UnityTest attribute to instantiate the MonoBehaviour you wish to test and wait for it to finish running. Implement the IMonoBehaviourTest interface on the MonoBehaviour to state when the test completes.

    Declaration
    public MonoBehaviourTest(bool dontDestroyOnLoad = true)
    Parameters
    Type Name Description
    Boolean dontDestroyOnLoad
    Examples
    [UnityTest]
    public IEnumerator MonoBehaviourTest_Works()
    {
        yield return new MonoBehaviourTest<MyMonoBehaviourTest>();
    }
    
    public class MyMonoBehaviourTest : MonoBehaviour, IMonoBehaviourTest
    {
        private int frameCount;
        public bool IsTestFinished
        {
            get { return frameCount > 10; }
        }
    
        void Update()
        {
            frameCount++;
        }
    }

    Properties

    component

    A MonoBehaviour component created for the test and attached to the tests GameObject.

    Declaration
    public T component { get; }
    Property Value
    Type Description
    T

    gameObject

    A GameObject created as a container for the test component.

    Declaration
    public GameObject gameObject { get; }
    Property Value
    Type Description
    GameObject

    keepWaiting

    (Inherited) Returns `true`` if the test is not finished yet, which keeps the coroutine suspended

    Declaration
    public override bool keepWaiting { get; }
    Property Value
    Type Description
    Boolean
    Overrides
    CustomYieldInstruction.keepWaiting
    Back to top Copyright © 2022 Unity Technologies
    Generated by DocFX
    on Tuesday, July 19, 2022
    Terms of use