Search code examples
c#unit-testingunity-game-engineunity-test-tools

Unity Test Tools Unit Test with GameObject gives "You are trying to create a MonoBehaviour using the 'new' keyword. This is not allowed."


I am using the Unity Test Tools framework. My unit test code looks like this (More included)

[TestFixture]
public class ActionMasterTester: MonoBehaviour{
    private ActionMaster actionMaster;

    [SetUp]
    public void SetUp()
    {
        actionMaster = new ActionMaster();
    }
}

The ActionMaster looks like this:

public class ActionMaster : MonoBehaviour {
}

If I run a test, then I get the following warning on the like actionMaster = new ActionMaster();

You are trying to create a MonoBehaviour using the 'new' keyword.  This is not allowed.  MonoBehaviours can only be added using AddComponent().  Alternatively, your script can inherit from ScriptableObject or no base class at all
UnityEngine.MonoBehaviour:.ctor()
ActionMaster:.ctor()
ActionMasterTester:SetUp() (at Assets/Editor/ActionMasterTester.cs:21)
System.Reflection.MethodBase:Invoke(Object, Object[])
NUnit.Core.Reflect:InvokeMethod(MethodInfo, Object, Object[])
NUnit.Core.Reflect:InvokeMethod(MethodInfo, Object)
NUnit.Core.TestMethod:RunSetUp()
NUnit.Core.TestMethod:RunTest()
NUnit.Core.NUnitTestMethod:RunTest()
NUnit.Core.TestMethod:RunRepeatedTest()
NUnit.Core.TestMethod:RunTestInContext()
NUnit.Core.TestMethod:Run(EventListener, ITestFilter)
NUnit.Core.TestSuite:RunAllTests(TestResult, EventListener, ITestFilter)
NUnit.Core.TestSuite:RunSuite(EventListener, ITestFilter)
NUnit.Core.TestSuite:RunSuiteInContext(EventListener, ITestFilter)
NUnit.Core.TestSuite:Run(EventListener, ITestFilter)
NUnit.Core.TestFixture:Run(EventListener, ITestFilter)
NUnit.Core.TestSuite:RunAllTests(TestResult, EventListener, ITestFilter)
NUnit.Core.TestSuite:RunSuite(EventListener, ITestFilter)
NUnit.Core.TestSuite:RunSuiteInContext(EventListener, ITestFilter)
NUnit.Core.TestSuite:Run(EventListener, ITestFilter)
NUnit.Core.TestSuite:RunAllTests(TestResult, EventListener, ITestFilter)
NUnit.Core.TestSuite:RunSuite(EventListener, ITestFilter)
NUnit.Core.TestSuite:RunSuiteInContext(EventListener, ITestFilter)
NUnit.Core.TestSuite:Run(EventListener, ITestFilter)
UnityTest.NUnitTestEngine:ExecuteTestSuite(TestSuite, ITestRunnerCallback, TestFilter) (at Assets/UnityTestTools/UnitTesting/Editor/TestRunner/NUnitTestEngine.cs:139)
UnityTest.NUnitTestEngine:RunTests(TestFilter, ITestRunnerCallback) (at Assets/UnityTestTools/UnitTesting/Editor/TestRunner/NUnitTestEngine.cs:91)
UnityTest.UnitTestView:StartTestRun(TestFilter, ITestRunnerCallback) (at Assets/UnityTestTools/UnitTesting/Editor/TestRunner/TestRunner.cs:84)
UnityTest.UnitTestView:RunTests(TestFilter) (at Assets/UnityTestTools/UnitTesting/Editor/TestRunner/TestRunner.cs:53)
UnityTest.UnitTestView:RunTests() (at Assets/UnityTestTools/UnitTesting/Editor/TestRunner/TestRunner.cs:35)
UnityTest.UnitTestView:OnGUI() (at Assets/UnityTestTools/UnitTesting/Editor/TestRunner/UnitTestView.cs:79)
UnityEditor.DockArea:OnGUI()

I've tried simply adding a component, by actionMaster = gameObject.GetComponent<ActionMaster>();, but I get a Null Point Error. How can I make this work without a warning?


Solution

  • MonoBehaviour's are expected to be instantiated into the scene on a GameObject - so you can't just call new because that isn't related to a game object.

    To test a component derived from MonoBehaviour you need to create either instantiate a prefab (which has your component on it), or you need to create (or use an existing) GameObject and add your component:

    GameObject go = new GameObject();
    go.AddComponent<ActionMaster>();
    

    Note: Whilst this is technically the answer to your question, you need to be aware that when you run the above, the Awake() call is run immeadiately, but the Start() and Update() will not run until the next frame - and your tests could be all over by that point.