Forum Libraries, Components, and Plugins

CCSprites in unit tests

I'm trying to unit test a class which is creating a CCSprite from an image in the constructor. The constructor crashes with a nullpointer exception when running it in the unit test.

I assume that something needs to be initialized before loading a sprite. Any suggestions? I don't need the sprite's image itself, but I need to be able to instantiate the class.

Posts

  • RamiTabbaraRamiTabbara AUMember, Xamarin Team Xamurai

    Hi Jahn,

    I think providing a code snippet of what you're currently doing would be the best way to help you out.

  • Indeed. This little code sample illustrates it. Say that I want to test the GameEngine class:

    public class GameEngine
    {
      public CCSprite Creature { get; private set; }
    
      public GameEngine()
      {
        this.Creature = new CCSprite("creature.png");
      }
    
      public void PointTapped(CCPoint position)
      {
        this.Creature.Position = position;
      }
    }
    

    The test is as follows:

    [TestFixture]
    public class GameEngineTest
    {
      [Test]
      public void PointTapped_Called_MovesCreature()
      {
        // Arrange
        var gameEngine = new GameEngine();
    
        var tapPosition = new CCPoint(1, 2);
    
        // Act
        gameEngine.PointTapped(tapPosition);
    
        // Assert
        Assert.That(gameEngine.Creature.Position, Is.EqualTo(tapPosition));
      }
    }
    
    I'm getting a NullReferenceException:
    
    System.NullReferenceException : Object reference not set to an instance of an object.
       at CocosSharp.CCTexture2D.InitWithFile(String file) in c:\kjpgit\CocosSharp\src\textures\CCTexture2D.cs: line 378
       at CocosSharp.CCTextureCache.AddImage(String fileimage) in c:\kjpgit\CocosSharp\src\textures\CCTextureCache.cs: line 362
       at CocosSharp.CCSprite.InitWithFile(String fileName, Nullable`1 rectInPoints) in c:\kjpgit\CocosSharp\src\sprite_nodes\CCSprite.cs: line 366
       at CocosSharp.CCSprite..ctor(String fileName, Nullable`1 texRectInPixels) in c:\kjpgit\CocosSharp\src\sprite_nodes\CCSprite.cs: line 313
       at GameEngine..ctor() in CreatureTest.cs: line 31
       at GameEngineTest.PointTapped_Called_MovesCreature() in CreatureTest.cs: line 47
    

    The GameEngine class works fine in the app itself.

  • RamiTabbaraRamiTabbara AUMember, Xamarin Team Xamurai
    edited November 2015

    Hi Jahn,

    Normally, in initialising your game, which in 1.7.0.0-pre1 onwards translates to creating your CCGameView, a content manager is created that maintains a reference to your game content. So in particular, when you call new CCSprite("creature.png") it knows where the resource is located and is then subsequently able to load the texture.

    The problem with your unit test is that you have short-circuited all of this, and are attempting to create a sprite without an underlying game view.

    So one possible workaround would be to have an initial setup for your unit tests where a dummy scratch game view is created, that would allow you to temporarily add/remove the game objects such as sprites that are specific to each of your unit tests.

  • Hi RamiTabbara,

    Yes, I was thinking that something was not initialized. Initializing a new CCApplication solves the problem:

              var blp = new CCApplication();
              blp.Initialize();
    

    The only drawback is that the test takes a little longer to start (300 ms).

Sign In or Register to comment.