Hey Folks!
I’ve got question about writing a unit test involving interaction with the new unity input system.
Specifically as part of the unity open project, I thought I’d get to work with writing a unit test for this class (the input reader) https://github.com/UnityTechnologies/open-project-1/blob/master/UOP1_Project/Assets/Scripts/InputReader.cs#L34-L39
public void OnAttack(InputAction.CallbackContext context)
{
if (attackEvent != null
&& context.phase == InputActionPhase.Started)
attackEvent.Invoke();
}
I thought this would be a good example test:
[Test]
public void InputAttack_CallsAttackFunction()
{
Mock<UnityAction> action = new Mock<UnityAction>();
action.Setup(a => a.Invoke());
inputReader.attackEvent += action.Object;
// Can't mock a struct
InputAction.CallbackContext context = new Mock<InputAction.CallbackContext>();
/* NOT SHOWN: setup a getter on the mock that returns
InputActionPhase.Started under context.phase */
inputReader.OnAttack(context);
action.Verify(a => a.Invoke(), Times.Once());
}
If you look at the code it requires an InputAction.CallbackContext to be passed in. Unfortunately, InputAction.CallbackContext is a STRUCT which cannot be mocked.
I was wondering if anyone out there had some thoughts on how to test a function that takes an InputAction.CallbackContext