1

I am doing some unit test exercises using a HTML5/JS game I wrote and JSUnit test runner. I am satisfied by the simplicity of it but that simplicity goes even in the docs as there is no explanation what trully assertEquals() does.

I made a stub aka fake object of my preloader and I want to check its state against this fake preloader.

I used assertEquals(gamePreloader, myPreloader) but the test fails with error:

Expected <[object Object]> (Object) but was <[object Object]> (Object)

Meaning they are not the same objects.

Does assertEquals() checks for the state of the objects or does it checks in memory that those two objects are actually the same object?

Vlad
  • 2,739
  • 7
  • 49
  • 100
  • 1
    I know that qunit has `deepEqual`, I am not sure if jsunit has something similar. – epascarello Feb 16 '16 at 14:34
  • Well thanks for reminding me that QUnit exists. JSUnit seems really plain :) though it was really easy to set up. No it doesn't have deepEqual. – Vlad Feb 16 '16 at 14:40
  • Though web documentation is poor, when you download the package, search to docs folder, it contains the API docs. And yes JSUnit community is not so active in past 6 years, probably because other frameworks are more popular. Still JUnit has solid tools to test. – Vlad Feb 19 '16 at 11:02
  • SOme of the API is not covered in the docs, like JsUnit.Params which gives way of making parametrized tests – Vlad Feb 19 '16 at 11:12

1 Answers1

2

The assertEquals will perform an equal-equal-equal operation, so it will do:

 gamePreloader === myPreloader

In that case, it will only return true if the objects are exactly the same, and not if they have the same values. If you want to test some object's property's value, you must specifically test it, e.g:

assertEquals(gamePreloader.status, myPreloader.status)

And if you want to test all the values, then you will need a loop, or something like that:

Object.keys(gamePreloader).forEach(function(key) {
  assertEquals(gamePreloader[key], myPreloader[key]);
});
Buzinas
  • 11,597
  • 2
  • 36
  • 58
  • Good point. According to the Unit Testing rules each part of a state should be tested in a separate test. Is using loop to test each property allowed in javascript testing. What does your experience says? Yes I can make another question for this topic, but I am just curious. – Vlad Feb 16 '16 at 14:37
  • I don't see a problem of testing more than one property of an object, if they all are similar to the same test purpose. – Buzinas Feb 16 '16 at 15:20