简体   繁体   中英

Qunit parameterized tests and mocking

I have two questions:

Can you have parameterised unit tests in qunit?

How do you do mocking with qunit eg mocking a getJSON call?

Thanks

For mocking ajax requests, you can try something like this...

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

Instead of overriding jQuery's AJAX function, you could also use the jQuery Mockjax plugin developed by .appendTo. 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({
    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/ ). The only thing to keep in mind is that this will only mock jQuery ajax calls, not all XMLHttpRequests. If you want to do that, then follow @bertvh's advice and use Sinon.js.

I just started using Sinon.JS , which allows mocking of XMLHttpRequests and also offers a simple fake-server API. Really easy to use! It also offers integration with qunit.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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