Search Unity

Question How to Unit Test Aspects?

Discussion in 'Entity Component System' started by dsfgddsfgdsgd, Jan 28, 2023.

  1. dsfgddsfgdsgd

    dsfgddsfgdsgd

    Joined:
    Jan 24, 2014
    Posts:
    16
    I'm trying to write a unit test for an aspect
    GridAspect


    Code (CSharp):
    1.     public readonly partial struct GridAspect : IAspect
    2.     {
    3.        
    4.         private readonly RefRW<GridItems> _gridItems;
    5.        
    6.         private readonly RefRO<GridProperties> _gridProperties;
    7.  
    8.         /// <summary>
    9.         ///
    10.         /// </summary>
    11.         /// <param name="index"></param>
    12.         private Entity this[int index]
    13.         {
    14.             get => _gridItems.ValueRO.Items[index];
    15.  
    16.             set => _gridItems.ValueRW.Items[index] = value;
    17.         }
    18.  
    19.         /// <summary>
    20.         ///
    21.         /// </summary>
    22.         /// <param name="index"></param>
    23.         /// <param name="item"></param>
    24.         /// <returns></returns>
    25.         public bool TryGetItem(int index, out Entity item)
    26.         {
    27.             if (InArray(index))
    28.             {
    29.                 item = this[index];
    30.                 return true;
    31.             }
    32.             item = default;
    33.             return false;
    34.         }
    35.  
    36.         /// <summary>
    37.         ///
    38.         /// </summary>
    39.         /// <param name="index"></param>
    40.         /// <param name="entity"></param>
    41.         public bool TrySetItem(int index, Entity entity)
    42.         {
    43.             if (InArray(index))
    44.             {
    45.                 this[index] = entity;
    46.                 return true;
    47.             }
    48.             return false;
    49.         }
    50.        
    51.         /// <summary>
    52.         /// Check to make sure index is in the
    53.         /// </summary>
    54.         /// <param name="index"></param>
    55.         /// <returns></returns>
    56.         public bool InArray(int index) => 0 <= index && index < Count;
    57.  
    58.     }
    But Im not sure how to mock out
    RefRW
    RefRO
    or how to set the read-only fields

    Is there a framework for testing Aspects?
     
  2. jamwils

    jamwils

    Joined:
    Jun 6, 2017
    Posts:
    2
    Hey, I leverage a "Test World" in my unit tests so I can use a lot of the different objects in ECS. Here is a simple way to create and test a simple aspect.

    Code (CSharp):
    1. public class SimpleTests {
    2.     private EntityManager _entityManager;
    3.  
    4.     [SetUp]
    5.     public void SetUp() {
    6.         World.DefaultGameObjectInjectionWorld = new World("Test World");
    7.         _entityManager = World.DefaultGameObjectInjectionWorld.EntityManager;
    8.     }
    9.  
    10.     [TearDown]
    11.     public void TearDown() {
    12.         World.DefaultGameObjectInjectionWorld.Dispose();
    13.     }
    14.  
    15.     [Test]
    16.     public void TestAspectPosition() {
    17.         var entity = _entityManager.CreateEntity();
    18.         _entityManager.AddComponentData(entity, new Position {
    19.             Value = new float3(0, 0, 0),
    20.         });
    21.  
    22.         var aspect = _entityManager.GetAspectRO<PositionAspect>(entity);
    23.         var data = aspect.GetPosition();
    24.  
    25.         Assert.AreEqual(new float3(0, 0, 0), data);
    26.     }
    27. }