javascript - Why in Jasmine, we cannot put the expect in an outside function? -
if using jasmine 1.3.1, use
describe("trytry", function() { var i; function checkforsituation(a) { // say, if made function because // there lot of processing console.log("there", a); expect(foo(3, a)).toequal( 3 + ); } (i = 0; < 5; i++) { console.log("here", i); it("should add " + i, function() { checkforsituation(i); }); } });
and foo just:
function foo(a, b) { return + b; }
i expect check 0 4, , print out
here 0 there 0 here 1 there 1 ...
but instead, print in chrome's console as: here 0, here 1, ...
, there 5
5 times. know why expect
cannot put in outside function , in jasmine if need put many steps function?
as side note, in javascript, feel whole new language developed , can won't work -- , wonder can done prevent type of things happening, without knowing happen.
if you'd try out, on https://github.com/jianlin/jasmine-looping-an-it-calling-function-that-does-expect
i've never used jasmine, don't know of does, , don't know it
does, seems common closure problem in javascript.
this happens when asynchronous (like event
handler) tries access iterator (i
) in callback. time, loop has finished , i
last value. using closure, value of i
captured , passed properly. if it
asynchronous, won't able order of console.log
results wanted - "here" come first, "there".
you try code:
it("should add " + i, (function(i) { return function () { checkforsituation(i); }; })(i);
Comments
Post a Comment