Trouble testing isNaN

Aug 16, 2011 at 11:27 AM

Hi. I've tried out Jint for a few hours and finally banged my head against the wall trying to figure out testing a callback from our method.

This works:

        public void BasicTest()
            string expectedValue = "http://restservices.localhost";

            var jint = new JintEngine();
            var returnVal = jint.Run(File.ReadAllText("api.js") + "return $api.getRestHost();");

            Assert.AreEqual(expectedValue, returnVal);

which is great.


However - when I try to test our Person.get function the internal Javascript function isNaN doesn't work the way I expect.

First the Javascript function:


$api.Person.get = function (id, callback) {
    if (isNaN(id)) {
        callback({ code: 400, message: "Invalid ID" });
    //Do stuff and call external service


...and the C# test

public void Test3()
    var jint = new JintEngine();
    jint.SetFunction("callback", new Action(() => {}));
    var returnVal = jint.Run(File.ReadAllText("api.js") + "return $api.Person.get(abc, callback);");

//Assert not implemented - should test that I get JSON back with a 'code' property set to 400 }

The if-statement in $api.Person.get doesn't evaluate to true - so my callback with code 400 isn't invoked and I later on get
an error and a huge stacktrace because the "Do stuff" section at some point depends on the Javascript 'window' object (which obviously isn't there).

How should the test be written in order for isNaN to work like expected?
Aug 16, 2011 at 6:50 PM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.
Aug 16, 2011 at 6:52 PM

If you take the dev branch from the source code it should fix the issue. Thanks for reporting.

Aug 17, 2011 at 8:42 AM
Edited Aug 17, 2011 at 8:42 AM

Thanks - it works like a charm. I made the following test and it passes.

Sidenote: Your API works quite well - the ceremony of testing function calls is less than expected. Keep up the good work


public void GetPerson_IdIsNotANumber_ReturnsCode400()
    int returnCode = -1;
    int expectedValue = 400;
    var jint = new JintEngine();
    jint.SetFunction("test", new Action<JsObject>(parameters =>
                                                    returnCode = parameters["code"].ToInteger();
    jint.Run(File.ReadAllText("api.js") + "return $api.Person.get('abc', function(obj) {test(obj)});");
    Assert.AreEqual(expectedValue, returnCode);
Aug 17, 2011 at 11:32 PM

You seem happy ... so why this very bad review on the homepage ?

Aug 18, 2011 at 11:20 AM

Bad review??? I think I gave a 4 star out of 5 - the last star was because a file was missing... :o)

...before submitting I checked my rating and somehow I've submitted a two star vote. It should have been 4 from the beginning - thanks for noticing

Regards Kristian

Aug 18, 2011 at 4:22 PM

No worry, thanks.