Jump to content

Unit Testing my Game with JSUnit


spinnerbox
 Share

Recommended Posts

I have this test:

function testLoadAtlasJSONHash_CreateCustomJSONAtlasAndRunTheFunc_PassesIfThePhaserLoaderContainsTheAssetKey () {
                var keyName = 'KEY_NAME',
                    keyValue = 'keyValue',
                    sheetUrl = 'assets/testing/images/guidesAndBackgrounds.png', 
                    sheetConfigUrl = 'assets/testing/settings/guidesAndBackgroundsHash.json';

                WML.addConstant(WML.ImageAssetKeys, keyName, keyValue);
                game.load.atlasJSONHash(WML.ImageAssetKeys[keyName], sheetUrl, sheetConfigUrl);

                assertTrue(game.load.checkKeyExists(Phaser.Loader.TEXTURE_ATLAS_JSON_HASH, keyName));
            }

Note: I have separate tests which pass for WML.ImageAssetKeys[keyName] and for WML.addConstant()

And I am not sure where it is failing. checkKeyExists() returns false. 

How can I know or find why it returns false? 

Link to comment
Share on other sites

Well after some fiddling I found what is the problem. The loader needs time to load the asset, so the assertion must be run after the file finishes loading. Here is the test which passes.

function testLoadAtlasJSONHash_CreateCustomJSONAtlasAndRunTheFunc_PassesIfThePhaserLoaderContainsTheAssetKey () {
                var keyName = 'KEY_NAME',
                    keyValue = 'keyValue',
                    sheetUrl = 'assets/testing/images/guidesAndBackgrounds.png', 
                    sheetConfigUrl = 'assets/testing/settings/guidesAndBackgroundsHash.json',
                    fileCompleteListener= function () {
                        assertTrue(game.load.checkKeyExists(Phaser.Loader.TEXTURE_ATLAS_JSON_HASH, keyName));
                    };

                WML.loadAtlasJSONHash(keyName, keyValue, sheetUrl, sheetConfigUrl).onFileComplete.add(fileCompleteListener);
            }

Yes this doesn't follow the Arrange-Act-Assert rule but it works.

loadAtlasJSONHash = function (keyName, keyValue, sheetUrl, sheetConfigUrl) {
            WML.addConstant(WML.ImageAssetKeys, keyName, keyValue);
            return game.load.atlasJSONHash(WML.ImageAssetKeys[keyName], sheetUrl, sheetConfigUrl);
        };

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...