I am trying to test my impact game with Jasmine. I added a canvas to the SpecRunner.html file, and imported impact.js and main.js.
I was able to get ig.version = "1.22" to test.
Now, how do I access the Game object and my other entities? I tried ig.Game, ig.game, ig.main, ig.MyGame and all show undefined or null.
Thanks for your help everyone.
1 decade ago
by TommyBs
have you tried just MyGame?
The issue is that all Impact instances are locked away within closures. If you take a look at MyGame, it's an ig.Class constructor. What you'll have to do is expose your game object to window within your game's init:
init: function () {
// logic
window.MyTestInstance = this;
}
Didn't work. Any other ideas?
Thanks for your help.
1 decade ago
by drhayes
Your spec file is going to have to mock out the Impact module functions:
ig.module
,
.requires
, and
.defines
. Don&
039;t worry too much about what gets passed to #ig.module
and
.requires
. Call the function that gets passed to
.defines
and you've got your Entity (or whatever) globally available.
Here's a test file I wrote for my Event Chain library. See how I mocked
module
,
requires
, and
define
? That should work for you, too.
Everything in Impact ends up being global, one way or another. When you declare an entity like this:
BadGuyEntity = ig.Entity.extend({...
See how there&
039;s no #var
at the beginning? Boom, global variable.
Since almost everything in Impact works like that, you&
039;ll have to mock anything your entity (or whatever you're testing) needs, expose it globally, then call the function passed to #defines
... and, only then, start your tests.
Make sense?
drhayes, you are a Java Ninja, but I still don't fully understand. Your explanation is perfect, but I am novice+ at javascript.
First, if BadGuyEntity is global anyway, why can't I see it without having to do anything in the first place?
I will look at the excellent code you provided and try to figure it out.
Thank you so much for your help.
1 decade ago
by drhayes
Hey, if the explanation was perfect then novices would get it the first time. The burden's on me, don't worry about it. ( =
It&
039;s only global once the function you pass to #defines
has executed. But let me back up a bit...
Do you know JS? You know how, normally, everything you define in a JS file and include in a
<script>
tag is exposed to everything else? Dominic (the guy who wrote Impact) is trying to fix that. That&
039;s why you have to #ig.module
and
.requires
and
.defines
in all your files. That way, Impact can start with your game file and then "walk around" looking at all the files in your game&
039;s #.requires
list in order to figure out what it needs to load next... and then, before loading
those, it looks in
their .requires
lists... and on and on until everything is loaded.
That brings us back to the file you're trying to test... and good on you for testing, BTW.
In Impact, the engine won&
039;t run the function you pass to #defines
until it has satisfied all the
requires
. Only once that function runs will the global be defined.
You don&
039;t want to do that in your test, though. You just want to test what you want to test... which is why you need to figure out how to run the function passed to #defines
.
That's what this line does. Only once that function has run will the stuff that&
039;s defined inside of it be exposed globally (because of the missing #var
, right?).
Make sense?
I think I got it. You must be good if you can teach an idiot like me. :-)
Thanks again for your help.
Jasmine is all well and good, but are you aware that QUnit has the ability to test pixels in canvas? That's compelling...
oh heck Ashkettle, now I have to go learn something else. :=)
Thanks for the info.
Page 1 of 1
« first
« previous
next ›
last »