Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Automatic Testing Scenario in Unity
namespace MHS {
interface ISpinner {
public void spin(Vector3 value);
}
[Serializable]
public SpinController { //we can instantiate this in a test
public float speed;
public Vector3 axis;
private ISpinner spinner;
public void setSpinController(ISpinController spinner) {
this.spinner = spinner;
}
public void doStuffBasedOnTime(float time) {
spinner.spin(time);
}
}
public Spinner : GameBehavior, ISpinner { // we cannot instantiate this in a test
public SpinController controller;
void OnEnable() {
controller.SetSpinController(this);
}
void Update() {
controller.doStuffBasedOnTime(Time.deltaTime);
}
//ISpinner implementation:
public void spin(Vector3 value) {
transform.Rotate(value);
}
}
}
namespace MHS.Tests {
[TestFixture]
public class SpinnerTest {
}
[Test]
public void objectSpinsBasedOnTimeAndAxis() {
//arrange
SpinController controller = ...;
ISpinner spinner = ...;
controller.speed = 5f;
controller.axis = new Vector3(0,1,0);
controller.SetSpinner(spinner);
//act
controller.doStuffBasedOnTime(0.02f);
//assert
Assert.AreEqual(spinner.rotation, controller.axis*controller.speed);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment