Call OnTriggerEnter2D from unit test
Hey guys!
I'm stuck trying to make some unit testing on Unity and its kind frustrating because the docs is very superficial.
I'm trying to test this piece of code:
public class AimForTarget : MonoBehaviour
{
void OnTriggerEnter2D(Collider2D collider)
{
if (collider.gameObject.tag == "Player")
{
SceneManager.LoadScene(0);
}
else if (collider.gameObject.tag != "ENEMY")
{
GameObject.Destroy(gameObject);
}
}
}
My first try was to make an integration test for this using Unity Test Tools, but I ran into a problem, when the transform hits the player it was loading the scene 0, which breaks Unity Test Tools, so I couldn't find any instruction about how to mock SceneManager to avoid this behaviour.
So, I decide to move forward with unit test and I'm struggling into this:
[Test]
public void Should_call_OnTriggerEnter2D()
{
var gameObject = new GameObject();
gameObject.AddComponent<AimForTarget>();
Collider2D collider = new Collider2D();
gameObject.BroadcastMessage("OnTriggerEnter2D", collider);
AimForTarget aimForTarget = gameObject.GetComponent<AimForTarget>();
//some assert from aim From Target
}
How to trigger OnTriggerEnter2D from gameObject properly?
Thanks in advance!
Your answer
Follow this Question
Related Questions
Unfortunately, (GameName) has stopped (report ,ok) 0 Answers
Unity 2018 Test Runner Broken? 4 Answers
Collision working once 3 Answers
Unity Test Tools Not Working 0 Answers