Skip to content

Instantly share code, notes, and snippets.

@charlieamat
Last active August 30, 2017 13:34
Show Gist options
  • Save charlieamat/d61ac2c5f69855539b2f544b3f50ecfb to your computer and use it in GitHub Desktop.
Save charlieamat/d61ac2c5f69855539b2f544b3f50ecfb to your computer and use it in GitHub Desktop.
using UnityEngine;
using UnityEngine.Events;
public class Goal : MonoBehaviour {
public Players scoresTo;
public IScoreEvent scoreEvent;
public void Construct(Players scoresTo, IScoreEvent scoreEvent) {
this.scoresTo = scoresTo;
this.scoreEvent = scoreEvent;
}
public void Awake() {
this.Construct(this.scoresTo, this.scoreEvent);
}
public void OnTriggerEnter2D(Collider2D other) {
if(other.tag == Tags.BALL) scoreEvent.Invoke(scoresTo);
}
}
using UnityEngine;
using UnityEditor;
using UnityEngine.Events;
using UnityEngine.TestTools;
using NUnit.Framework;
using NSubstitute;
using System.Collections;
public class GoalTest {
public class OnTriggerEnter2D {
private Goal goal;
private IScoreEvent scoreEvent;
private BoxCollider2D ball;
[SetUp]
public void BeforeEachTest() {
goal = new GameObject().AddComponent<Goal>();
scoreEvent = Substitute.For<IScoreEvent>();
ball = new GameObject().AddComponent<BoxCollider2D>();
goal.Construct(Players.ONE, scoreEvent);
}
[Test]
public void Does_Nothing_When_Collider_Tag_Is_Not_Ball() {
goal.OnTriggerEnter2D(ball);
scoreEvent.DidNotReceive().Invoke(Arg.Any<Players>());
}
[Test]
public void Calls_ScoreEvent_When_Collider_Tag_Is_Ball() {
ball.tag = Tags.BALL;
goal.OnTriggerEnter2D(ball);
scoreEvent.Received().Invoke(Arg.Any<Players>());
}
}
}
public interface IScoreEvent
{
void Invoke(Players arg);
}
[System.Serializable]
public class ScoreEvent : UnityEvent<Players>, IScoreEvent
{
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment