Pregunta

Using JsUnit, I am trying to make use of the assertEquals function, such that when a suite of tests fail, they will print useful information as to why they failed.

My current situation is such:

let actual = some_method_call ();
let expected = [
        {num: 1, file: f, id: "apples"},
        {num: 6, file: f, id: "bananas"},
        {num: 9, file: f, id: "accurate_comparison"}
        ];
assertEquals (expected, actual);

Unfortunately, iterating through the exception leaves me with only:

isJsUnitException: true
comment: null
jsUnitMessage: Expected [object Object],[object Object],[object Object] (object) but was 42 (number)
stackTrace: > JsUnitException:307
> _assert:113
> assertEquals:150
> testStealBitcoins:105
> anonymous:159

How can I get the Expected line to intelligently display the contents of my objects that are expected? Any solution would need to be applicable to various layers of nested objects. Also, is this the wrong way to approach failing test-debugging?

¿Fue útil?

Solución

Creating a wrapper for the assertEquals is helpful and straightforward. The trick that I didn't understand was that Javascript doesn't truly support an equals method or even a toString method that I was used to with Java.

To print the details of a variable the following is a pretty decent way to go about it:

if (typeof x === "object" && x !== null) {
    print (JSON.stringify(x));
}
else {
    print (String(x));
}

Console.log could be used instead of print if GJS is not being used. JSON stringify turns the object into its JSON representation (as a String). It isn't accurate for the sake of type checking, but if that is not your concern, this works pretty well.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top