Qunit parameterized tests and mocking

后端 未结 5 996
一生所求
一生所求 2020-12-23 09:38

I have two questions:

Can you have parameterised unit tests in qunit?

How do you do mocking with qunit e.g. mocking a getJSON c

5条回答
  •  忘掉有多难
    2020-12-23 10:15

    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

提交回复
热议问题