0

I'm following along with Test Driven Javascript Development and just finshed setting up jstestdriver. I'm receiving odd errors and failed test messages when running failing tests, for example:

TestCase("ArrayTest", {
  "test array splice should not modify array": function () {
    var arr = [1, 2, 3, 4, 5];
    var result = arr.splice(2, 3);
    assertEquals([1, 2, 3, 4, 5], arr);
  }
});

My failed test output is fairly succinct and includes an odd FailureException error:

F
Total 1 tests (Passed: 0; Fails: 1; Errors: 0) (3.00 ms)
  Chrome 27.0.1453.116 Mac OS: Run 1 tests (Passed: 0; Fails: 1; Errors 0) (3.00 ms)
    ArrayTest.test array splice should not modify array failed (3.00 ms):

null
com.google.jstestdriver.FailureException
    at com.google.jstestdriver.FailureCheckerAction.run(Unknown Source)
    at com.google.jstestdriver.ActionRunner.runActions(Unknown Source)
    at com.google.jstestdriver.JsTestDriverServer.main(Unknown Source)

I would like to know if it's possible to fix that failure exception, and also receive a message such as “expected [1, 2, 3, 4, 5] but was [1, 2]", as the book mentions you should receive.

mcabrams
  • 686
  • 1
  • 6
  • 23

1 Answers1

0

Yes, sure it is possible.

There are at least two ways:

  1. to write your own comparator function for arrays
  2. to convert both arrays to string and compare them as a strings with assertEquals

For example:

assertEquals([1, 2, 3, 4, 5] + "", arr + "");

To fix the failing test you need to use slice instead of splice

So the code of the test will look like:

var arr = [1, 2, 3, 4, 5];
var result = arr.slice(2, 3);
assertEquals([1, 2, 3, 4, 5], arr);

If you would like to have some custom message when this assert fails you can write it as a first argument:

assertEquals("message text", [1, 2, 3, 4, 5], arr);
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post - you can always comment on your own posts, and once you have sufficient [reputation](http://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](http://stackoverflow.com/help/privileges/comment). – Jan Doggen Feb 03 '14 at 12:35
  • Please, could you correct me if I'am wrong: Q: I would like to know if it's possible to fix that failure exception. A: Yes, sure it is possible. Should I provide some sample of code? – Iaroslav Karandashev Feb 03 '14 at 14:06