Search Unity

Question Loading ref on [UnitySetUp] make test fail

Discussion in 'Testing & Automation' started by Nefisto, Sep 10, 2022.

  1. Nefisto

    Nefisto

    Joined:
    Sep 17, 2014
    Posts:
    335
    Can someone explain to me why this work

    Code (CSharp):
    1. public class InputListenerTests : InputTestFixture
    2. {
    3.     private InputListener inputListener;
    4.  
    5.     [UnityTest]
    6.     public IEnumerator When_PressW_SelectedHighBattleActionEventRuns()
    7.     {
    8.         // Arrange
    9.         inputListener = new GameObject().AddComponent<InputListener>();
    10.         yield return inputListener.LoadBattleActions();
    11.        
    12.         var keyboard = InputSystem.AddDevice<Keyboard>();
    13.         var hasTriggeredHighEvent = false;
    14.         inputListener.OnSelectBattleAction += ctx =>
    15.         {
    16.             hasTriggeredHighEvent = string.Equals(ctx.BattleAction.name, GameConstants.BattleActions.HIGH_NAME);
    17.         };
    18.  
    19.         // Act
    20.         Press(keyboard.wKey);
    21.         yield return null;
    22.        
    23.         // Assert
    24.         Assert.That(hasTriggeredHighEvent, Is.True);
    25.     }
    26. }
    And this does not
    Code (CSharp):
    1. public class InputListenerTests : InputTestFixture
    2. {
    3.     private InputListener inputListener;
    4.    
    5.     [UnitySetUp]
    6.     public IEnumerator Init()
    7.     {
    8.         inputListener = new GameObject()
    9.             .AddComponent<InputListener>();
    10.    
    11.         yield return inputListener.LoadBattleActions();
    12.     }
    13.  
    14.     [UnityTest]
    15.     public IEnumerator When_PressW_SelectedHighBattleActionEventRuns()
    16.     {
    17.         // Arrange
    18.         var keyboard = InputSystem.AddDevice<Keyboard>();
    19.         var hasTriggeredHighEvent = false;
    20.         inputListener.OnSelectBattleAction += ctx =>
    21.         {
    22.             hasTriggeredHighEvent = string.Equals(ctx.BattleAction.name, GameConstants.BattleActions.HIGH_NAME);
    23.         };
    24.  
    25.         // Act
    26.         Press(keyboard.wKey);
    27.         yield return null;
    28.        
    29.         // Assert
    30.         Assert.That(hasTriggeredHighEvent, Is.True);
    31.     }
    32. }