如何测试在GWT中使用正确参数调用的某些JavaScript?
我围绕现有的JavaScript API构建了一个精简的GWT包装器。javascriptapi是独立测试的,所以我只想测试GWT包装器是否使用正确的参数调用正确的JavaScript函数。关于如何着手做这件事有什么想法吗 目前,GWTAPI有一堆公共方法,经过一点处理后,这些方法调用私有的本机方法,这些方法进行JavaScript API调用如何测试在GWT中使用正确参数调用的某些JavaScript?,javascript,testing,gwt,Javascript,Testing,Gwt,我围绕现有的JavaScript API构建了一个精简的GWT包装器。javascriptapi是独立测试的,所以我只想测试GWT包装器是否使用正确的参数调用正确的JavaScript函数。关于如何着手做这件事有什么想法吗 目前,GWTAPI有一堆公共方法,经过一点处理后,这些方法调用私有的本机方法,这些方法进行JavaScript API调用 感谢您的指导。确定函数是否已启动的最佳方法是将函数写入闭包,然后测试闭包的值。由于闭包是一个变量,您可以将其定义为函数参数,但该定义必须发生在父函数上。
感谢您的指导。确定函数是否已启动的最佳方法是将函数写入闭包,然后测试闭包的值。由于闭包是一个变量,您可以将其定义为函数参数,但该定义必须发生在父函数上。确定函数是否已触发的最佳方法是使函数写入闭包,然后测试闭包的值。由于闭包是一个变量,您可以将其定义为函数参数,但该定义必须发生在父函数上。在java世界中,您要求的通常是使用委托和接口来完成的 我将创建一个(java)接口,该接口与js库提供的API一一对应,然后创建该接口的简单实现 然后,包装器代码将包装接口。在测试期间,您将使用自己的接口替换该接口的实现,其中每个方法仅声明是否调用它 例如
在java世界中,您所要求的通常是使用委托和接口来完成的 我将创建一个(java)接口,该接口与js库提供的API一一对应,然后创建该接口的简单实现 然后,包装器代码将包装接口。在测试期间,您将使用自己的接口替换该接口的实现,其中每个方法仅声明是否调用它 例如
custom.lib.js has these exported methods/objects:
var exports = {
method1: function(i) {...},
method2: function() {...},
...etc
}
your custom interface:
public interface CustomLib {
String method1(int i);
void method2();
//...etc etc
}
your simple impl of CustomLib:
public class CustomLibImpl implements CustomLib {
public CustomLibImpl() {
initJS();
}
private native void initJS()/*-{
//...init the custom lib here, e.g.
$wnd.YOUR_NAME_SPACE.customlib = CUSTOMLIB.newObject("blah", 123, "fake");
}-*/;
public native String method1(int i)/*-{
return $wnd.YOUR_NAME_SPACE.customlib.method1(i);
}-*/;
void method2()/*-{
$wnd.YOUR_NAME_SPACE.customlib.method2();
}-*/;
//...etc etc
}
then in your Wrapper class:
public class Wrapper {
private CustomLib customLib;
public Wrapper(CustomLib customLib ) {
this.customLib = customLib;
}
public String yourAPIMethod1(int i) {
return customLib.method1(i);
}
///etc for method2()
}
your test:
public class YourCustomWrapperTest {
Wrapper wrapper;
public void setup() {
wrapper = new Wrapper(new CustomLib() {
//a new impl that just do asserts, no jsni, no logic.
public String method1(int i) {assertCalledWith(i);}
public void method2() {assertNeverCalledTwice();}
//etc with other methods
});
}
public void testSomething() { wrapper.yourAPIMethod1(1);}
}