Zenject使用時のテストが難しい(6月22日)

今日やったこと

ワーキングメモリーレーニン

勉強

ゲーム開発

PlayerViewの作成

Player(球)のViewと、そのテストを作成した。

途中ZenjectのPlayModeテストでエラーが出たので、解決策を調べてQiitaに投稿した。

テストを書くことは結構大変だと感じた

qiita.com

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Zenject;

public class PlayerView : MonoBehaviour, IPlayerView
{
    private Transform tr;
    public Transform Tr => tr;

    [Inject]
    private void Initialize()
    {
        tr = this.GetComponent<Transform>();
        UpdatePosition(new Vector3(0,1,0));
    }

    //Transformの更新
    public void UpdatePosition(Vector3 pos){
        this.tr.position = pos;
    }
}

public interface IPlayerView {
    Transform Tr{get;}
    void UpdatePosition(Vector3 pos);
}
using System.Collections;
using System.Collections.Generic;
using NUnit.Framework;
using UnityEngine;
using UnityEngine.TestTools;
using Zenject;

namespace Tests
{
    [TestFixture]
    public class PlayerViewTest : ZenjectIntegrationTestFixture
    {
        GameObject playerPrefab;
        IPlayerView playerView;
        IPlayerView playerViewInstance;
        IFactory<IPlayerView> playerPrefabFactory;


        [SetUp]
        public void SetUp(){

            PreInstall();

            playerPrefab = new GameObject();
            playerView = playerPrefab.AddComponent<PlayerView>();

            Container
                .BindIFactory<IPlayerView>()
                .To<PlayerView>()
                .FromComponentInNewPrefab(playerPrefab);

            Container
                .Bind<IPlayerView>()
                .To<PlayerView>()
                .AsTransient();

            playerPrefabFactory = Container.Resolve<IFactory<IPlayerView>>();

            PostInstall();
        }

        [Test]
        public void InitializeTest(){

            Assert.IsNotNull(playerPrefabFactory);
            Assert.IsNull(playerViewInstance);
            playerViewInstance = playerPrefabFactory.Create();
            Assert.IsNotNull(playerViewInstance);

            Assert.AreEqual(new Vector3(0,1,0),playerViewInstance.Tr.position);
        }

        // A Test behaves as an ordinary method
        [Test]
        public void UpdatePositionTest()
        {
            // Use the Assert class to test conditions
            playerViewInstance = playerPrefabFactory.Create();
            playerViewInstance.UpdatePosition(new Vector3(1,1,0));
            Assert.AreEqual(1,playerViewInstance.Tr.position.x);
            playerViewInstance.UpdatePosition(new Vector3(2,1,2));
            Assert.AreEqual(new Vector3(2,1,2),playerViewInstance.Tr.position);
        }
    }
}

JINS MEME ESの測定結果

f:id:alberto_hojo:20190623010323p:plain:w250

作業時間 8h10m

集中時間 2h42m

集中率 33%

感想

  • テスト書くのは結構大変

  • 頭痛はだいぶよくなったけど、集中するとまた再発しそう