使用GameObject的Unity测试工具单元测试给出了#34;您正在尝试使用' new'来创建MonoBehaviour。关键词。这是不允许的。"

时间:2015-11-07 00:21:52

标签: c# unit-testing unity3d unity-test-tools

我正在使用Unity Test Tools框架。我的单元测试代码如下所示(更多内容)

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

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

ActionMaster看起来像这样:

public class ActionMaster : MonoBehaviour {
}

如果我运行测试,那么我会收到类似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()

我尝试过简单地按actionMaster = gameObject.GetComponent<ActionMaster>();添加一个组件,但是我得到了一个空点错误。如何在没有警告的情况下完成这项工作?

2 个答案:

答案 0 :(得分:4)

MonoBehaviour预计将在GameObject中实例化到场景中 - 所以你不能只调用new,因为它与游戏对象无关。

要测试从MonoBehaviour派生的组件,您需要创建实例化预制件(其上包含您的组件),或者您需要创建(或使用现有的)GameObject并添加您的组件:

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

注意:虽然这在技术上是你的问题的答案,你需要知道,当你运行上面的时,唤醒()调用是立即运行,但Start()和Update()不会一直运行到下一帧 - 你的测试可以在那时完成。

答案 1 :(得分:0)

@ peterept的答案大多是正确的,但实际上它在Hierarchy中创建了对象。更好的方法是实际创建一个名为ActionMaster的对象,然后在代码中找到它,如下所示:

actionMaster= GameObject.FindObjectOfType<ActionMaster>();