Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We’re making changes to the Unity Runtime Fee pricing policy that we announced on September 12th. Access our latest thread for more information!
    Dismiss Notice
  3. Dismiss Notice

Question Why I cannot store InputSystem.AddDevice<Keyboard>() into SetUp when testing?

Discussion in 'Input System' started by Nefisto, Sep 8, 2022.

  1. Nefisto

    Nefisto

    Joined:
    Sep 17, 2014
    Posts:
    323
    This throws a Null Exception
    Code (CSharp):
    1. private InputListener inputListener;
    2. private Keyboard virtualKeyboard;
    3.  
    4. [UnitySetUp]
    5. public IEnumerator Init()
    6. {
    7.     inputListener = new GameObject("Input listener").AddComponent<InputListener>();
    8.     yield return inputListener.LoadBattleActions();
    9.    
    10.     virtualKeyboard = InputSystem.AddDevice<Keyboard>();
    11. }
    12.  
    13. [UnityTest]
    14. public IEnumerator InputListener_WhenRunningBattleActions_TriggerCorrectEvents()
    15. {
    16.     // Arrange
    17.  
    18.     // Act
    19.     Press(virtualKeyboard.wKey);
    20.  
    21.     // Assert
    22.     yield return null;
    23. }
    upload_2022-9-8_10-46-7.png

    And this does not
    Code (CSharp):
    1. private InputListener inputListener;
    2. private Keyboard virtualKeyboard;
    3.  
    4. [UnitySetUp]
    5. public IEnumerator Init()
    6. {
    7.     inputListener = new GameObject("Input listener").AddComponent<InputListener>();
    8.     yield return inputListener.LoadBattleActions();
    9.    
    10. }
    11.  
    12. [UnityTest]
    13. public IEnumerator InputListener_WhenRunningBattleActions_TriggerCorrectEvents()
    14. {
    15.     // Arrange
    16.     virtualKeyboard = InputSystem.AddDevice<Keyboard>();
    17.  
    18.     // Act
    19.     Press(virtualKeyboard.wKey);
    20.  
    21.     // Assert
    22.     yield return null;
    23. }
    upload_2022-9-8_10-47-13.png

    Why?