标签: unity-test-tools

Unity 测试工具使用 GameObject 进行单元测试给出“您正在尝试使用 'new' 关键字创建 MonoBehaviour。这是不允许的。”

我正在使用 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, …
Run Code Online (Sandbox Code Playgroud)

c# unit-testing unity-game-engine unity-test-tools

2
推荐指数
1
解决办法
2665
查看次数