如何使用ZenjectIntegrationTestFixture?它和ZenjectSceneTestFixture有什么区别?

时间:2019-04-18 11:00:12

标签: c# unit-testing unity3d zenject

我有以下要测试的Controller,该Controller继承了MonoBehaviour类,并且是OrderApp的一部分,OrderApp也属于Monobehaviour,也需要实例化App才能访问View成员变量(当验证base.RegisterRecording是否正确调用时)参数)

我先尝试使用ZenjectUnitTestFixture,但失败了,因为我无法在编辑模式下实例化App游戏对象 因此,在搜索之后,我发现ZenjectIntegrationTestFixture用于播放模式,因此我可以为App创建游戏对象

您可以在“测试”功能中看到。当我运行此testMethod时,在Assert.That(!ProjectContext.HasInstance);时在PreInstall()中失败。

为什么会失败?我采用错误的方法吗?

我也找不到关于PreInstall和PostInstall的良好文档,说明它们的作用和工作原理

public class OrderApp : BaseApp
{
    public OrderView View;
    public OrderController Controller;
    protected override void Awake()
    {
        base.Awake();
    }
    protected override void Start()
    {
        base.Start();
    }
    protected override void Update()
    {
        base.Update();
    }
}

public class OrderController : BaseController
{
    Dependency1 obj1;
    Dependency2 obj2;
    Dependency3 obj3;
    public OrderApp  App
    {
        get { return FindObjectOfType<OrderApp>(); }
    }
    [Inject]
    public void Initialize(D1 ,D2,D3)
    {
        obj1= D1;
        obj2= D2;
        obj3= D3;
    }
    public override void Start()
    {
        base.Start();
        base.RegisterRecording(App.View.Recorder,App.View.Player);
        obj2.AddSubscriber(this);
        GetOrder();
    }
}
public class TestOrderContollerPlayMode : ZenjectIntegrationTestFixture
{
     OrderApp testApp;

     [UnityTest]
     public IEnumerator RunTest1()
     {
         #region Arrange
         var orderViewMock = new Mock<OrderView>();
         ZenjectLocator.Container.Bind<OrderView>().FromInstance(orderViewMock .Object);

         var dependency1Mock = new Mock<Dependency1>();
         var dependency2Mock = new Mock<Dependency2>();
         var dependency3Mock = new Mock<Dependency3>();

         OrderController controller = new OrderController();
         controller.Initialize(dependency1Mock.Object, dependency1Mock.Object, dependency1Mock.Object);

         #endregion

         PreInstall();

         ZenjectLocator.Container.Bind<OrderController>().FromInstance(controller).AsSingle();

         PostInstall();

         #region Act 

         GameObject testObject = GameObject.Instantiate(new GameObject());
         testObject.AddComponent<OrderApp>();
         testApp = testObject.GetComponent<kOrderApp>();

         #endregion

         #region Assert

         Assert.NotNull(testApp.Controller.App.View.Recorder);
         voiceRecorderMock.Verify(vrm => vrm.RegisterForVoiceRecording (It.Is<Dependency1>(ar => RecorderMatch(ar, controller.App.View.Recorder)),It.Is<AudioPlayer>(ap => PlayerMatch(ap, controller.App.View.SpeechPlayer)), Times.Once);

         #endregion

         yield break;
     }

     bool RecorderMatch(AudioRecorder ar, AudioRecorder speechRecorder)
     {
         return ar.Equals(speechRecorder);
     }

     bool PlayerMatch(AudioPlayer ap, AudioPlayer speechPlayer)
     {
         return ap.Equals(speechPlayer);
     }
 }

0 个答案:

没有答案