2

I'm using Jasmine standalone https://github.com/jasmine/jasmine/releases

I have declared a global variable global_song in SpecRunner.html (I can access it from chrome console so it's truly global) which includes script where I am trying to concatenate global_song to "should be able to play Song " :

it("should be able to play Song " + global_song, function() {
player.play(song);
expect(player.currentlyPlayingSong).toEqual(global_song);

//demonstrates use of custom matcher
expect(player).toBePlaying(song);
});

Why it cannot access global_song variable ?

Update : expect(player.currentlyPlayingSong).toEqual(global_song) works whereas it("should be able to play Song " + global_song doesn't work.

Andriy Ivaneyko
  • 20,639
  • 6
  • 60
  • 82
user310291
  • 36,946
  • 82
  • 271
  • 487

2 Answers2

0

Where have you defined global_song? If you did that in the beforeEach() function this behaviour would make sense as the code in the describe block (which attempts to define your it() function) gets executed before the beforeEach() as described in this other SO answer.

Community
  • 1
  • 1
Matthijs Brouns
  • 2,299
  • 1
  • 27
  • 37
  • as I said I defined global_song in SpecRunner.html, proof I can access this var from chrome console – user310291 Mar 12 '16 at 21:17
  • expect(player.currentlyPlayingSong).toEqual(global_song) works whereas it("should be able to play Song " + global_song doesn't work – user310291 Mar 12 '16 at 21:21
0

Well i suppose your global_song created after executing of

player.play(song);

That's way it's not available in test as first parameter of it and available after executing player.play in expect(player.currentlyPlayingSong).toEqual(global_song) assertion.

Try to add simple assignment to global_song separatelly from player.play to verify that it's available before player.play executed:

window.global_song = 'value'

Just take a look on that sample to illustrate the main possible candidate of problem:

function foo(){
    window['bar'] = 'bar';
}

console.log(window.bar); // Undefined
foo(); // now window contain bar variable.
console.log(window.bar); // 'bar'
Andriy Ivaneyko
  • 20,639
  • 6
  • 60
  • 82