weixin_33713350 2017-07-31 14:22 采纳率: 0%
浏览 48

在QUnit中使用Mockjax?

I see this example in Mockjax docs:

$.mockjax({
  url: "/rest",
  data: function ( json ) {
    assert.deepEqual( JSON.parse(json), expected ); // QUnit example.
    return true;
  }
});

But I am not sure how should I use it with QUnit testing methods. Any ideas?

mockjax docs

I tried this but it says it expected at least one assertion, as if it doesn't run it at all, the assert line:

QUnit.test("mockjax test", function (assert) {
    $.mockjax({
        url: "/restful/fortune",
        data: function (json) {
            assert.deepEqual(JSON.parse(json), expected); // QUnit example.
            return true;
        },
        responseText: {
            status: "success",
            fortune: "Are you a mock turtle?"
        }
    });
});
  • 写回答

1条回答 默认 最新

  • weixin_33724059 2017-07-31 14:36
    关注

    You're close, but Mockjax emulates the async nature of Ajax requests which means you need to tell QUnit that this test is asynchronous and when it is complete. Additionally, you're not actually doing any Ajax calls, so the Mock handler would never get hit. You would need to put code in your test to actually test the ajax call (thus hitting the mock handler you have above):

    QUnit.test("mockjax test", function (assert) {
        // This is QUnit's callback for async testing
        let done = assert.async();
    
        // You also need to define the `expected` data
        let expected = { foo: "bar" };
    
        $.mockjax({
            url: "/restful/fortune",
            data: function (json) {
                assert.deepEqual(JSON.parse(json), expected); // QUnit example.
                return true;
            },
            responseText: {
                status: "success",
                fortune: "Are you a mock turtle?"
            }
        });
    
        // Now add the actual function call to your SOURCE code that you're testing...
        // Since I don't know your source code, I'll just put a jquery ajax call here
        $.ajax({
            url: "/restful/fortune",
            data: { foo: "bar" },
            complete: function() {
                done(); // now we tell QUnit that our test is complete.
            }
        });
    });
    

    I would encourage you to read QUnit's guide to unit testing, and the async documentation.

    评论

报告相同问题?

悬赏问题

  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥30 最小化遗憾贪心算法上界
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝