4

I am creating a basic tenpin bowling scoring program in JavaScript. To do this I have created the different functions Frame and Game so far. I have created a frame double for testing my game function. The test I am having problems on is this:

it('can take a frame as a parameter', function(){
frame.frameOne.and.callFake(function(){
[5, 5, 'spare'];
});
expect(game.frame1(frame)).toBe([5, 5, 'spare']);
});`    

and the game logic it is testing is this:

Game.prototype.frame1 = function (frame) {
frame.frameOne;
return [5, 5, 'spare'];
};

Now I'm pretty sure the code is probably all wrong being as I'm new to Javascript/Jasmine and coding in general but what I'm confused about is when I run the test I am getting this error from Jasmine:

Expected [ 5, 5, 'spare' ] to be [ 5, 5, 'spare' ]

Now to me that reads as a match, not a fail?!? I have swapped the arrays for the value true which then passes so why are the Arrays apparently an identical match but Jasmine is saying they are not and failing?

Ideas?!?

2 Answers2

3

toBe is basically strict comparison operation ===. In your case even though two arrays look the same, they are not equal, because they are two different objects. Non-primitive types in javascript (object, functions, arrays, etc.) are only equal if they point to the same object.

You should use toEqual instead which checks object equality by comparing properties and their values:

expect(game.frame1(frame)).toEqual([5, 5, 'spare']);
dfsq
  • 191,768
  • 25
  • 236
  • 258
0

Because in your code there are two different arrays, with the same values in both.

in java script comparing objects (like arrays) is doing a Reference comparison. You have two different array instances, not two references to the same array.

This link might help explain: Object Equality in JavaScript

Caleb
  • 1,088
  • 7
  • 7