Qunit: Check for Leak

I have a leak problem while testing my jQuery plugin. The problem arises when I want to make fun of a value or function on a literal object.

Example:

test('Overwrite some default setting', function(){
    $.fn.plugin.defaults.bar = 'foo';
});

test('Bar should be undefined', function(){
    equals( $.fn.plugin.defaults.bar, undefined );
});

This test fails because the first test added "bar" var to the default values. I fixed it with the following code, but the copy of the copy does not look very elegant.

$(function(){

    /*
     * Trolley Button Base: Options.
     */

    var defaults_copy = $.extend({}, $.fn.plugin.defaults );

    var setdown = {
        setup : function(){
            $.fn.plugin.defaults = $.extend({}, defaults_copy);
        },

        teardown : function(){ }
    };

    module('Test leakage', setdown );

    test('Overwrite some default setting', function(){
        $.fn.plugin.defaults.bar = 'foo';
    });

    test('Bar should be undefined', function(){
        equals( $.fn.plugin.defaults.bar, undefined );
    });

})

Also, if I have several objects in the jQuery namespace, this can become a little dirty if I need to take several copies of each object. So it was interesting if anyone has a better solution to "reset" all objects?

+3
source share
2 answers

QUnit. , . , - , , , :

test('Overwrite some default setting', function(){
    // test code
    $.fn.plugin.defaults.bar = 'foo';

    // cleanup code
    delete $.fn.plugin.defaults.bar;
});

test('Bar should be undefined', function(){
    equals( $.fn.plugin.defaults.bar, undefined );
});
+2

, , :

.   $ (document).ready(function() {

    // Test Setup/TearDown
    function codeUnderTestModuleTestSetup() {
        //any setup needed
    }

    function resetDefaults() {
        //code in here to reset defaults
    }

    function resetSomethingElse() {
        //code in here to reset something else
    }

    function codeUnderTestModuleTestTearDown() {
        resetDefaults();
        resetSomethingElse();
    }

    //Tests
    module('Your module title Test Harness');    

    test('FunctionUnderTest_Behaviour_ExpectedResult', 1, function () {
        codeUnderTestModuleTestSetup();

        //Arrange
        //code removed

        //Act
        //Code removed

        //Assert
        //Code removed

        codeUnderTestModuleTestTearDown();
    });

}

, .

0

Source: https://habr.com/ru/post/1778494/


All Articles