【问题标题】:Gwt junit test does not initialize $wnd.jQueryGwt junit 测试未初始化 $wnd.jQuery
【发布时间】:2015-08-18 07:37:07
【问题描述】:

inputmask 结合 gwt 的方式如下:

公共类 JQueryMask { public native static void setMask(Element elem,String mask) /*-{ $wnd.jQuery(elem).find("input").inputmask(mask); $wnd.jQuery(elem).find("input").change(function(e) { }); }-*/; public native static void removeMask(Element elem) /*-{ $wnd.jQuery(elem).find("input").inputmask("remove"); }-*/; public native static void setMaskWithGreadyFalse(Element elem,String mask) /*-{ $wnd.jQuery(elem).find("input").inputmask({mask:mask,greedy:false}); $wnd.jQuery(elem).find("input").change(function(e) { }); }-*/; }

当小部件初始化并且一切都在浏览器中运行时,会调用此方法。 jQuery 变量没有被初始化,这会产生同样的异常:

console.log($wnd.jQuery());

还有:

控制台.log($wnd.$());

这是:

com.google.gwt.core.client.JavaScriptException: (null) @assembly.gwtlib.gui.plugins.JQueryMask::removeMask(Lcom/google/gwt/dom/client/Element;)([JavaScript object(77)] ): 空值 在 com.google.gwt.dev.shell.BrowserChannelServer.invokeJavascript(BrowserChannelServer.java:249) 在 com.google.gwt.dev.shell.ModuleSpaceOOPHM.doInvoke(ModuleSpaceOOPHM.java:136) 在 com.google.gwt.dev.shell.ModuleSpace.invokeNative(ModuleSpace.java:576) 在 com.google.gwt.dev.shell.ModuleSpace.invokeNativeVoid(ModuleSpace.java:304) 在 com.google.gwt.dev.shell.JavaScriptHost.invokeNativeVoid(JavaScriptHost.java:107) 在 assembly.gwtlib.gui.plugins.JQueryMask.removeMask(JQueryMask.java) 在 assembly.gwtlib.gui.widget.input.Input.setMask(Input.java:54) 在 assembly.gwtlib.gui.widget.input.DateInput.(DateInput.java:76) ...

【问题讨论】:

    标签: jquery gwt junit jquery-inputmask


    【解决方案1】:

    GWTTestCases 使用它们自己的 HTML 主机页面(因为,从技术上讲,你甚至可能没有,或者不是静态的),所以如果你的测试需要 jQuery,你必须从测试本身加载它(例如使用ScriptInjector,无论是来自您的测试方法还是来自gwtSetUp()

    【讨论】:

      【解决方案2】:

      感谢 Thomas Broyer 我已经实现了以下代码:

      回调 c=new Callback() { @Override public void onSuccess(Void result) { ScriptInjector.fromUrl("urlTo/jquery.inputmask.js").setWindow(ScriptInjector.TOP_WINDOW).setCallback(new Callback<Void, Exception>() { @Override public void onSuccess(Void result) { //Your tests } @Override public void onFailure(Exception reason) { reason.printStackTrace(); junit.framework.Assert.fail(reason.getMessage()); } }).inject(); } @Override public void onFailure(Exception reason) { reason.printStackTrace(); junit.framework.Assert.fail(reason.getMessage()); } }; ScriptInjector.fromUrl("urlTo/jQuery-2.1.4.min.js").setWindow(ScriptInjector.TOP_WINDOW).setCallback(c).inject();//setWindow(ScriptInjector.TOP_WINDOW) is important

      【讨论】:

      • 它仍然不能完全工作,但我认为它在掩码框架中,即:$wnd.jQuery(elem).find("input").inputmask("");and $wnd. jQuery(elem).find("input").inputmask("remove");工作但 $wnd.jQuery(elem).find("input").inputmask("9");不o.O
      • 我没有设法让它工作。我的解决方法是 :if(!$wnd.jQuery){return;} 在本机方法中
      猜你喜欢
      • 2013-01-16
      • 2011-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-27
      • 2020-07-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多