1

Closed

Trouble testing isNaN

description

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:
    [TestMethod]
    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" });
    return;
}
//Do stuff and call external service
};




...and the C# test


[TestMethod]
public void Test3()
{
var jint = new JintEngine();
jint.SetFunction("callback", new Action(() => {}));
jint.SetDebugMode(true);
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?
Closed Oct 3, 2014 at 6:43 PM by sebastienros
Jint has been rewritten and this issue should be fixed in the new version. Please check it on https://github.com/sebastienros/jint or on using the nuget package here: https://www.nuget.org/packages/Jint/

comments

sebastienros wrote Aug 16, 2011 at 6:52 PM

Fixed in changeset 40adefe21e4b