【问题标题】:GWT rpc callback does not call after calling in GWTTestCase在 GWTTestCase 中调用后 GWT rpc 回调不调用
【发布时间】:2013-01-07 19:13:58
【问题描述】:

我写了一个这样的 GWTTestCase:

public void testClickButton() {
    SampleView view = new SampleView();
    RootPanel.get().add(view);
    view.textBox.setText("Saeed Zarinfam");

    assertEquals("", view.label.getText());

//        ButtonElement.as(view.button.getElement()).click();
    view.button.getElement().<ButtonElement>cast().click();

    assertEquals("Bean \"OCTO\" has been created", view.label.getText());
}

当我运行这个测试时,它连接到我的 servlet(我在我的 servlet 上添加了一些日志)但 RPC 回调没有在我的SampleView 中调用,junit 说:

expected: <Bean "OCTO" has been created>, actual: <>

这是我在按钮点击处理程序中的回调:

@UiHandler("button")
void onClick(ClickEvent e) {

    labelTest.setText("click button");

    AsyncCallback<FooBean> callback = new AsyncCallback<FooBean>() {
        public void onFailure(Throwable caught) {
            // Show the RPC error message to the user
            labelTest.setText("call fail");
            label.setText("Failure : " + caught.getMessage());
        }

        public void onSuccess(FooBean result) {
            labelTest.setText("call success");
            label.setText("Bean \"" + result.getName() + "\" has been created");
        }
    };

    // Make the call. Control flow will continue immediately and later
    // 'callback' will be invoked when the RPC completes.
    service.createBean("OCTO", callback);


}

为什么在这种情况下不调用 GWT rpc 回调?

【问题讨论】:

    标签: unit-testing gwt servlets gwttestcase


    【解决方案1】:

    即使在GWTTestCase 中,RPC 调用也是异步的。你必须调用delayTestFinish() 告诉运行程序测试是异步的,然后调用finish()“在未来的某个时间”告诉它已经完成并且OK(否则你会超时)。

    在您的情况下,由于调用代码无法知道 RPC 调用何时完成,您只能粗略猜测需要多少时间,并使用Timer
    如果您问我,请更好地重构您的代码以使其更具可测试性(注意:Selenium 的工作原理大致相同:每秒检查一次条件直到超时,http://seleniumhq.org/docs/02_selenium_ide.jsp#the-waitfor-commands-in-ajax-applications,就像您要重新安排的 Timer条件不满足最多N次)

    https://developers.google.com/web-toolkit/doc/latest/DevGuideTesting#DevGuideAsynchronousTesting

    【讨论】:

    • T hank 的 Thomas,我有一个问题,我已经为我的案例测试了 gwt-test-utils lib,它与 gwt rpc 一起工作很好,但我的 GWT ui 很复杂,它无法解析我的 * .ui.xml 文件,然后我选择 GWTTestCase,我选择正确的方式吗?
    • 我觉得 gwt-test-utils 太老套了。对于端到端测试,我想我宁愿采用 Selenium(或类似)方式,仅/主要使用 GWTTestCase 进行单元测试(小部件或其他基于 JSNI 的东西)。但实际上,我在测试应用程序方面相当糟糕(在测试库/API 方面要好得多)。
    猜你喜欢
    • 2014-05-15
    • 1970-01-01
    • 2011-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多