简体   繁体   English

Qunit参数化测试和模拟

[英]Qunit parameterized tests and mocking

I have two questions: 我有两个问题:

Can you have parameterised unit tests in qunit? 你能在qunit中进行参数化单元测试吗?

How do you do mocking with qunit eg mocking a getJSON call? 你如何使用qunit进行模拟,例如模拟getJSON调用?

Thanks 谢谢

For mocking ajax requests, you can try something like this... 对于模拟ajax请求,你可以试试这样的东西......

Here's the function you want to test: 这是您要测试的功能:

    var functionToTest = function () {
        $.ajax({
            url: 'someUrl',
            type: 'POST',
            dataType: 'json',
            data: 'foo=1&foo=2&foo=3',
            success: function (data) {
                $('#main').html(data.someProp);
            }
        });
    };

Here's the test case: 这是测试用例:

    test('ajax mock test', function () {
        var options = null;
        jQuery.ajax = function (param) {
            options = param;
        };
        functionToTest();
        options.success({
            someProp: 'bar'
        });
        same(options.data, 'foo=1&foo=2&foo=3');
        same($('#main').html(), 'bar');
    });

It's essentially overriding jQuery's ajax function and then checks the following 2 things: - the value that was passed to the ajax function - invokes the success callback and asserts that it did what it was supposed to do 它基本上覆盖了jQuery的ajax函数,然后检查以下两件事: - 传递给ajax函数的值 - 调用成功回调并声明它执行了它应该执行的操作

Instead of overriding jQuery's AJAX function, you could also use the jQuery Mockjax plugin developed by .appendTo. 您也可以使用.appendTo开发的jQuery Mockjax插件,而不是覆盖jQuery的AJAX函数。 This plugin essentially does what the other answer suggests, but it allows for much more sophisticated mocking. 这个插件基本上做了另一个答案所暗示的,但它允许更复杂的模拟。 For example, if you have the function: 例如,如果你有这个功能:

$.ajax({
    url:"/api/user",
    type:"GET",
    dataType:"json",
    data:'{"uid":"foobar"}',
    success:function(data){
        console.log("Success!");
    },
    error:function(data){
        console.log("Error!");
    }
});

You can mock it with mockjax by simply calling the function mockjax, which is automatically included in jQuery: 你可以通过简单地调用函数mockjax来模拟它,它可以自动包含在jQuery中:

$.mockjax({
    url:"/api/user",
    type:"GET",
    response:function(requestData){
         //handle the mock response in here
         this.responseText = '{"fullname":"Mr. Foo Bar"}';
    }
});

The second mocking function can be included in an external JavaScript file, say "mocks.js," and the only other thing that needs to be done is include the mockjax library (which can be found at https://github.com/appendto/jquery-mockjax/ ). 第二个模拟函数可以包含在外部JavaScript文件中,比如说“mocks.js”,唯一需要做的就是包含mockjax库(可以在https://github.com/appendto找到) / jquery-mockjax / )。 The only thing to keep in mind is that this will only mock jQuery ajax calls, not all XMLHttpRequests. 唯一要记住的是,这只会模拟jQuery ajax调用,而不是所有XMLHttpRequests。 If you want to do that, then follow @bertvh's advice and use Sinon.js. 如果你想这样做,那么按照@ bertvh的建议并使用Sinon.js。

I just started using Sinon.JS , which allows mocking of XMLHttpRequests and also offers a simple fake-server API. 我刚开始使用Sinon.JS ,它允许模拟 XMLHttpRequests并提供一个简单的假服务器API。 Really easy to use! 真的好用! It also offers integration with qunit. 它还提供与qunit的集成。

有我的addon实现允许参数化qunit测试: https//github.com/AStepaniuk/qunit-parameterize

请参阅此链接以模拟setup / teardown方法中的getJSON调用, http: //www.ajaxprojects.com/ajax/tutorialdetails.php?item = 505

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM