How to test functions within Javascriptcore?

余生长醉 提交于 2020-01-13 16:23:00

问题


I have seen many examples where someone takes a JavaScript function and evaluates it by doing something like:

    JSContext *context = [[JSContext alloc] initWithVirtualMachine:[[JSVirtualMachine alloc] init]];
    [context evaluateScript:@"var add = function(a, b) {return a + b;}"];
    NSLog(@"%@", [context[@"add"] callWithArguments:@[@20, @30]]); //Output is 50

But how can we run test cases on the function? By calling arguments we can see the result (assuming it doesn't throw an exception). In the event it does throw an exception we can set an exception handler on the context prior to calling the code.

But what is the best way for testing code, using something like Mocha or Chai within JavaScriptCore?

来源:https://stackoverflow.com/questions/58231125/how-to-test-functions-within-javascriptcore

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!