- Home /
Unit Testing MonoBehaviour.Assertion component not Working with property.
Everyone knows that you can't use new with MonoBehaviour. What I am doing is put everything that doesn't use the unity API inside another class then reference it. This left me with MonoBehaviours that uses only the other class. Is this the correct way to go.
Update
This time I had a problem with the integration tests. I have a code that goes like this.
class Foo : MonoBehavior{
public GameObject AGameObject{
get{ return bar.AGameObject}
set{ bar.AGameObject = value}
AnotherClass bar;
void Start(){
bar = new AnotherClass ();
}
}
I get when I use the Assertion Component:
Unexpected top level layout group! Missing GUILayout.EndScrollView/EndVertical/EndHorizontal? UnityEditor.DockArea:OnGUI()
Any Help ?
Answer by Lonsomehell · Jun 17, 2015 at 06:27 AM
For the update I was able to solve the problem. I have to test for null value.
class Foo : MonoBehavior{
public GameObject AGameObject{
get
{
If(bar == null)
return null;
return bar.AGameObject;
}
set
{
If(bar == null)
bar = new AnotherClass ();
bar.AGameObject = value;
}
AnotherClass bar;
void Start(){
if(bar == null)
bar = new AnotherClass ();
}
}
Answer by Tomek-Paszek · Jun 15, 2015 at 06:38 PM
Hey!
Your approach is fine. There is nothing wrong with decoupling game logic from Unity API :) I've written a blog post about it, check it out: http://blogs.unity3d.com/2014/06/03/unit-testing-part-2-unit-testing-monobehaviours/
Tomek
Good Day sir,Actually I was following your blog post.It is a great post. But testing like this felt kinda weird and unnatural. I am still a new developer and this is my first time writing unit tests. Any other advice would be great.
I understand that it might feel like your not doing it the easiest way and you're writing a lot of unnecessary code but for the moment there is no 'easy' way to unit test $$anonymous$$Bs. There is nothing wrong with writing a little bit more of the code in order to keep your logic decoupled. I think you will get used to it quickly and you will stop noticing it :)
Thanks for your reply. At least I know know that this is not going to be trivial at the beginning.
Your answer
Follow this Question
Related Questions
Unity Test Tools Integration Tests with Jenkins 1 Answer
FB.Apprequest problem 0 Answers
Dynamic Integration test error - Not found skipping/KeyNotFoundException 0 Answers
How to make MonoBehavior class testable with NSubstitute? 2 Answers
Unity Test Tools - Integration Test for existing scenes 1 Answer