Pea*_*oto 2 c# unit-testing unity-game-engine unity-test-tools
我正在使用 Unity 测试工具框架。我的单元测试代码看起来像这样(包括更多)
[TestFixture]
public class ActionMasterTester: MonoBehaviour{
private ActionMaster actionMaster;
[SetUp]
public void SetUp()
{
actionMaster = new ActionMaster();
}
}
Run Code Online (Sandbox Code Playgroud)
ActionMaster 看起来像这样:
public class ActionMaster : MonoBehaviour {
}
Run Code Online (Sandbox Code Playgroud)
如果我运行测试,那么我会收到以下警告 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()
Run Code Online (Sandbox Code Playgroud)
我试过简单地添加一个组件 by actionMaster = gameObject.GetComponent<ActionMaster>();,但出现空点错误。我怎样才能在没有警告的情况下完成这项工作?
MonoBehaviour 预计会在 GameObject 上实例化到场景中 - 因此您不能只调用 new,因为它与游戏对象无关。
要测试从 MonoBehaviour 派生的组件,您需要创建实例化预制件(其中包含您的组件),或者您需要创建(或使用现有的)游戏对象并添加您的组件:
GameObject go = new GameObject();
go.AddComponent<ActionMaster>();
Run Code Online (Sandbox Code Playgroud)
注意:虽然这在技术上是您问题的答案,但您需要注意,当您运行上述内容时,会立即运行 Awake() 调用,但 Start() 和 Update() 直到下一帧才会运行 -到那时,您的测试可能已经全部结束。