Unity Test Tools与GameObject的单元测试给出了“;您正试图使用';新';关键字.这是

本文关键字:这是 关键字 Unity GameObject Tools 单元测试 Test | 更新日期: 2023-09-27 18:20:59

我使用的是Unity测试工具框架。我的单元测试代码看起来像这样(更多包含)

[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>();简单地添加一个组件,但我得到了一个零点错误。我怎么能在没有警告的情况下完成这项工作?

Unity Test Tools与GameObject的单元测试给出了“;您正试图使用';新';关键字.这是

MonoBehavior的行为应该实例化到GameObject的场景中,所以你不能只调用new,因为它与游戏对象无关。

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

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

注意:虽然这在技术上是你问题的答案,但你需要意识到,当你运行上面的程序时,Awake()调用会立即运行,但Start()和Update()要到下一帧才会运行,到那时你的测试可能会全部结束

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

actionMaster= GameObject.FindObjectOfType<ActionMaster>();