我正在为 UnityEngine 项目使用 Zenject,我正在尝试为我的 MenuSystem 类编写单元测试,但我不断收到错误
System.Security.SecurityException : ECall methods must be packaged into a system module.
这是堆栈跟踪:
Stack Trace:
ProfileBlock.Start(String sampleName)
TypeAnalyzer.TryGetInfo(Type type) line 128
DiContainer.InstantiateInternal(Type concreteType, Boolean autoInject, List`1 extraArgs, InjectContext context, Object concreteIdentifier) line 1288
DiContainer.InstantiateExplicit(Type concreteType, Boolean autoInject, List`1 extraArgs, InjectContext context, Object concreteIdentifier) line 3303
TransientProvider.GetAllInstancesWithInjectSplit(InjectContext context, List`1 args, Action& injectAction, List`1 buffer) line 67
CachedProvider.GetAllInstancesWithInjectSplit(InjectContext context, List`1 args, Action& injectAction, List`1 buffer) line 100
IProviderExtensions.GetAllInstances(IProvider creator, InjectContext context, List`1 args, List`1 buffer) line 31
IProviderExtensions.GetAllInstances(IProvider creator, InjectContext context, List`1 buffer) line 22
DiContainer.GetDecoratedInstances(IProvider provider, InjectContext context, List`1 buffer) line 1199
DiContainer.SafeGetInstances(ProviderInfo providerInfo, InjectContext context, List`1 instances) line 1128
DiContainer.Resolve(InjectContext context) line 1053
DiContainer.ResolveId(Type contractType, Object identifier) line 2433
DiContainer.Resolve(Type contractType) line 2420
DiContainer.Resolve[TContract]() line 2415
MenuSystemTests.Setup() line 28
这是我的测试课:
using ArenaClient;
using ArenaClient.UI;
using NSubstitute;
using Zenject;
namespace ArenaClientTests
{
[TestFixture]
public class MenuSystemTests
{
private DiContainer _container;
private MenuSystem _menuSystem;
private IMenuViewController _menuViewController;
private ISettingsViewController _settingsViewController;
[SetUp]
public void Setup()
{
_container = new DiContainer();
_menuViewController = Substitute.For<IMenuViewController>();
_settingsViewController = Substitute.For<ISettingsViewController>();
_container.Bind<IMenuViewController>().FromInstance(_menuViewController);
_container.Bind<ISettingsViewController>().FromInstance(_settingsViewController);
_container.Bind<MenuSystem>().AsSingle();
_menuSystem = _container.Resolve<MenuSystem>();
}
[Test]
public void Init_ShouldCallInitViewOnControllers()
{
// Arrange
// Act
_menuSystem.Init();
// Assert
_menuViewController.Received(1).InitView();
_settingsViewController.Received(1).InitView();
}
}
}
这是 MenuSystem 类:
using ArenaClient.UI;
using Zenject;
namespace ArenaClient
{
public class MenuSystem : ISystem<MenuSystem>
{
[Inject]
private readonly IMenuViewController _menuViewController = null;
[Inject]
private readonly ISettingsViewController _settingsViewController = null;
public void Init()
{
_menuViewController.InitView();
_settingsViewController.InitView();
}
}
}
到目前为止我在 Visual Studio 中尝试过但没有成功: