I am having to unit test my project for my software engineering class and I am somewhat confused on if I am doing DI (Dependency Injection) correctly. I am very new to this whole process and it’s purely a learning experience for me. I have been told that it is far more difficult to test in games, so any advice will be greatly appreciated.
I have a InputController that requires a dependency on CPIM (CrossPlatformInputManager). I have set up an interface that eliminates the classes dependency on CPIM which if we do not provide an interface it sets up the interface with a class called MyCrossPlatformInputManager.
I want to know if I am doing DI correctly before writing interfaces for the rest of my scirpts and setting them up to be more testable?
Here is the scripts in question:
The playerInputController Class
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class playerInputController : MonoBehaviour {
public IplayerInputController inputInterface;
public playerInputController(IplayerInputController inputInterface)
{
this.inputInterface = inputInterface;
}
//this is here so that the script will run properly outside of testing.
public playerInputController() : base()
{
if (inputInterface == null)
{
inputInterface = new MyCrossPlatformInputManager();
}
}
//variables for Crossplatforminput
public float Lookh { get; set; }
public float Lookv { get; set; }
public float Moveh { get; set; }
public float Movev { get; set; }
public float f { get; set; }
playerController sendInput;
// Use this for initialization
void Start () {
anim = GetComponent<Animator>();
sendInput = GetComponent<playerController>();
}
public void getCurrentInput()
{
//left joystick or wasd
Moveh = inputInterface.GetAxisRaw("Horizontal");
Movev = inputInterface.GetAxisRaw("Vertical");
//right joystick or arrow keys
Lookh = inputInterface.GetAxisRaw("HorizontalLook");
Lookv = inputInterface.GetAxisRaw("VerticalLook");
//space or right trigger
f = inputInterface.GetAxisRaw("Fire1");
}
// Update is called once per frame
void FixedUpdate () {
//calls a method that gets our current input
getCurrentInput();
//send our inputs to the playercontroller
if (Mathf.Abs(Moveh) != 0 || Mathf.Abs(Movev) != 0) sendInput.updatePosition(Moveh, Movev);
//update player direction
if (Mathf.Abs(Lookh) != 0 || Mathf.Abs(Lookv) != 0) sendInput.updateDirection(Lookh, Lookv);
//gets if we are firing
if (Mathf.Abs(f) != 0) sendInput.fire();
if (inputInterface.GetAxisRaw("Grenade") != 0) UsePowerUp(2);
if (inputInterface.GetAxisRaw("Armor") != 0) UsePowerUp(0);
if (inputInterface.GetAxisRaw("SpeedBoost") != 0) UsePowerUp(1);
if (inputInterface.GetAxisRaw("Nuke") != 0) UsePowerUp(3);
}
void UsePowerUp(int i)
{
sendInput.UsePowerUp(i);
}
}
The IPlayerInputController Class
public interface IplayerInputController {
float GetAxisRaw(string axisName);
}
The MyCrossPlatformInputController class
using UnityStandardAssets.CrossPlatformInput;
public class MyCrossPlatformInputManager : IplayerInputController
{
public float GetAxisRaw(string axisName)
{
return CrossPlatformInputManager.GetAxisRaw(axisName);
}
}