AdamsLair / duality

a 2D Game Development Framework
https://adamslair.github.io/duality
MIT License
1.41k stars 290 forks source link

Things that could be improved regarding unit testability. #699

Open Barsonax opened 5 years ago

Barsonax commented 5 years ago

Summary

While writing unit tests for Singularity.Duality I came across some things that could be easier to unit test such as:

  1. Mocking/facking the Log class. This class currently does not implement a interface so I had to write a adapter to do so. If duality would have a ILog interface this would have prevented me from having to make a adapter class.
  2. Forgetting to unsubscribe from Scene.ComponentAdded and Scene.GameObjectsAdded might cause unpleasant side effects in other unit tests. This is basically because its is a type of static state. This wouldnt be a problem if these events were not static.

Thats pretty much it for now. Issue 2 might be harder to fix and also be possibly breaking as it probably requires a api change but issue 1 is really easy to do and can be added as of this moment I think.

Barsonax commented 5 years ago

Worked around the 2th issue by wrapping up those static events in a class that implements a interface so I could mock out that part. Not ideal but atleast my unit tests are now completely deterministic again (static state + parallel unit tests == alot of WTF moments). Still it would be alot nicer if some of those events where just instance events so I don't have to come up with workarounds.