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 安装CentOS6时卡住
  • ¥20 c语言写的8051单片机存储器mt29的模块程序
  • ¥60 求直线方程 使平面上n个点在直线同侧并且距离总和最小
  • ¥50 java算法,给定试题的难度数量(简单,普通,困难),和试题类型数量(单选,多选,判断),以及题库中各种类型的题有多少道,求能否随机抽题。
  • ¥50 rk3588板端推理
  • ¥250 opencv怎么去掉 数字0中间的斜杠。
  • ¥15 这种情况的伯德图和奈奎斯特曲线怎么分析?
  • ¥250 paddleocr带斜线的0很容易识别成9
  • ¥15 电子档案元素采集(tiff及PDF扫描图片)
  • ¥15 flink-sql-connector-rabbitmq使用