- Home /
Unit Testing Rigidbody2D without polluting scene with gameobjects
I just started using the Unity Unit Test suite, and can't figure out how to write tests that don't generate a bunch of a gameobjects. So, what's the suggested means of working with Unity's components like Rigidbody2D
and MonoBehavior
?
Answer by zerophase · May 26, 2014 at 11:06 PM
I managed to fix my issue by setting up a static GameObject
all tests use and a TearDown
method. Here's the code in case any could potentially be helped by it. I'm not sure if this is the most maintainable means of handling this issue, I'm a bit new to Unit testing.
Relevant code pieces:
private static GameObject gameObject = null;
//...
private GameObject MakeGamobjectWithRigidBody()
{
gameObject = new GameObject();
gameObject.AddComponent<Rigidbody2D>();
return gameObject;
}
//...
[TearDown]
public void DestroyGameObjects()
{
DestroyObject.DestroyImmediate(gameObject);
}
Your answer
Follow this Question
Related Questions
NUnit and VisualStudio 2 Answers
NUnit delayed constraint does not appear to work in playmode test 0 Answers
MSTools instead of NUnit 0 Answers
Unity 5.6 Unit Testing 1 Answer
Reasons to not use playmode unit tests? 0 Answers