#StackBounty: #c# #unity3d How to setup a new instance of InputAction.CallbackContext to test the input?

Bounty: 100

I’m using the new input system and invoke Unity events to pass the input to my bevaviour scripts. This is sample code showing a simple movement via input

public class MovementBehaviour : MonoBehaviour
{
    public void Move(InputAction.CallbackContext inputContext)
    {
        Vector2 movementDirection = inputContext.ReadValue<Vector2>();
        transform.position += new Vector3(movementDirection.x, movementDirection.y, transform.position.z);
    }
}

I want to test it in my unit tests but for this I have to pass in a new
InputAction.CallbackContext. So my sample unit test may look like

[TestFixture]
public class MovementBehaviourTests
{
    [Test]
    public void ItShouldMove()
    {
        GameObject gameObject = new GameObject();
        MovementBehaviour movementBehaviour = gameObject.AddComponent<MovementBehaviour>();
        
        // movementBehaviour.Move(); // pass in Vector2.right
        
        Assert.AreEqual(gameObject.transform.position, Vector3.right);
    }
}

Unfortunately I wasn’t able to figure out how to setup the new instance to pass in a Vector2.right input so that I can test the output.

https://docs.unity3d.com/Packages/c….InputSystem.InputAction.CallbackContext.html

Are there any value properties I can set? Do I have to perform any actions?


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.