c# - Unity 测试工具使用 GameObject 进行单元测试给出 "You are trying to create a MonoBehaviour using the ' new' 关键字。这是不允许的。”

标签 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();
    }
}

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>(); ,但我得到一个空点错误。我怎样才能在没有警告的情况下完成这项工作?

最佳答案

MonoBehaviour 预计会实例化到游戏对象的场景中 - 因此您不能只调用 new,因为这与游戏对象无关。

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

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

注意:虽然这在技术上是您问题的答案,但您需要注意,当您运行上述代码时,Awake() 调用会立即运行,但 Start() 和 Update() 不会运行直到下一帧 - 到那时您的测试可能就结束了。

关于c# - Unity 测试工具使用 GameObject 进行单元测试给出 "You are trying to create a MonoBehaviour using the ' new' 关键字。这是不允许的。”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33577637/

相关文章:

unity-game-engine - 3D播放器和2D地板。我有碰撞问题

unity-game-engine - 使用 setPixel() 时出现错误结果

c# - Grpc.Core.RpcException : 'Status (StatusCode = "Unavailable", Detail = "启动 gRPC 调用时出错

C# 属性,是否可以绕过定义 get 而不定义 set(无支持变量)?

spring - 配置 Spring MockMvc 以在内置参数解析器之前使用自定义参数解析器

java - 如何使用 Spring 在单元测试中模拟远程 REST API?

c# - 获取主组 ID

c# - .net 随机生成器是如何实现的?

java - 如何mock jcabi注解参数

unity-game-engine - 如何安全存储有关应用内购买/Unity3D 的信息