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