Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 207531
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T17:44:39+00:00 2026-05-11T17:44:39+00:00

I was wandering if it’s possible to mock a Game object in order to

  • 0

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?

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T17:44:39+00:00Added an answer on May 11, 2026 at 5:44 pm

    There are some good posts in that forum on the topic of unit testing. Here’s my personal approach to unit testing in XNA:

    • Ignore the Draw() method
    • Isolate complicated behavior in your own class methods
    • Test the tricky stuff, don’t sweat the rest

    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.

    [TestFixture]
    public class EntityTest {
        [Test]
        public void testMovement() {
            float speed = 1.0f; // units per second
            float updateDuration = 1.0f; // seconds
            Vector2 moveVector = new Vector2(0f, 1f);
            Vector2 originalPosition = new Vector2(8f, 12f);
    
            Entity entity = new Entity("testGuy");
            entity.NextStep = moveVector;
            entity.Position = originalPosition;
            entity.Speed = speed;
    
            /*** Look ma, no Game! ***/
            entity.Update(updateDuration);
    
            Vector2 moveVectorDirection = moveVector;
            moveVectorDirection.Normalize();
            Vector2 expected = originalPosition +
                (speed * updateDuration * moveVectorDirection);
    
            float epsilon = 0.0001f; // using == on floats: bad idea
            Assert.Less(Math.Abs(expected.X - entity.Position.X), epsilon);
            Assert.Less(Math.Abs(expected.Y - entity.Position.Y), epsilon);
        }
    }
    

    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:

    public class Entity {
        public Vector2 Position { get; set; }
        public Drawable Drawable { get; set; }
    
        public void Update(double seconds) {
            // Entity Update logic...
            if (Drawable != null) {
                Drawable.Update(seconds);
            }
        }
    
        public void LoadContent(/* I forget the args */) {
            // Entity LoadContent logic...
            if (Drawable != null) {
                Drawable.LoadContent(seconds);
            }
        }
    }
    

    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, ParticleEffectNonInteractiveEntity and AnimatedSpriteNoninteractiveEntity. Instead, I can do this:

    Entity torch = new NonInteractiveEntity();
    torch.Drawable = new AnimatedSpriteDrawable("Animations\litTorch");
    SomeGameScreen.AddEntity(torch);
    
    // let's say you can load an enemy AI script like this
    Entity enemy = new AIEntity("AIScritps\hostile");
    enemy.Drawable = new AnimatedSpriteDrawable("Animations\ogre");
    SomeGameScreen.AddEntity(enemy);
    

    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.

    public abstract class Drawable {
        // my game is 2d, so I use a Point to draw...
        public Point Coordinates { get; set; }
        // But I usually store my game state in a Vector2,
        // so I need a convenient way to convert. If this
        // were an interface, I'd have to write this code everywhere
        public void SetPosition(Vector2 value) {
            Coordinates = new Point((int)value.X, (int)value.Y);
        }
    
        // This is overridden by subclasses like AnimatedSprite and ParticleEffect
        public abstract void Draw(SpriteBatch spriteBatch, Rectangle visibleArea);
    }
    

    The subclasses define their own Draw logic. In your tank example, you could do a few things:

    • Add a new entity for each bullet
    • Make a TankEntity class which defines a List, and overrides Draw() to iterate over the Bullets (which define a Draw method of their own)
    • Make a ListDrawable

    Here’s an example implementation of ListDrawable, ignoring the question of how to manage the list itself.

    public class ListDrawable : Drawable {
        private List<Drawable> Children;
        // ...
        public override void Draw(SpriteBatch spriteBatch, Rectangle visibleArea) {
            if (Children == null) {
                return;
            }
    
            foreach (Drawable child in children) {
                child.Draw(spriteBatch, visibleArea);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was wandering how it's possible to reverese string s that are contained in
I was wandering if is it possible to customize the facebook registration plugin layout.
I was wandering how does elixir\sqlalchemy get to know all the entity classes I've
I was wandering if it's possible to create a user on a postgres database
Good afternoon I was wandering if this is possible to do with adobe air.
I was wandering if it's possible to open a source code in text editor
I am currently learning ARM Assembly and I was wandering whether I could test
I'm wandering myself what component is the best for displaying fast search results in
I was wandering how can I find out if an object already exists in
Just wandering will it be possible to partially string replace in jquery? I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.