I was wandering if it’s possible to mock a Game object in order to test my DrawableGameComponent component?
I know that mocking frameworks need an interface in order to function, but I need to mock the actual Game object.
edit: Here is a link to respective discussion on XNA Community forums.
Any help?
There are some good posts in that forum on the topic of unit testing. Here’s my personal approach to unit testing in XNA:
Here’s an example of a test to confirm that my Update method moves Entities the right distance between Update() calls. (I’m using NUnit.) I trimmed out a couple lines with different move vectors, but you get the idea: you shouldn’t need a Game to drive your tests.
Edit: Some other notes from the comments:
My Entity Class:
I chose to wrap all my game objects up in a centralized Entity class, that looks something like this:
This gives me a lot of flexibility to make subclasses of Entity (AIEntity, NonInteractiveEntity…) which probably override Update(). It also lets me subclass Drawable freely, without the hell of n^2 subclasses like
AnimatedSpriteAIEntity,ParticleEffectNonInteractiveEntityandAnimatedSpriteNoninteractiveEntity. Instead, I can do this:My Drawable class: I have an abstract class from which all my drawn objects are derived. I chose an abstract class because some of the behavior will be shared. It’d be perfectly acceptable to define this as an interface instead, if that’s not true of your code.
The subclasses define their own Draw logic. In your tank example, you could do a few things:
Here’s an example implementation of ListDrawable, ignoring the question of how to manage the list itself.