2
\$\begingroup\$

I have a mixin function which is as follows -

function mixin(receiver, supplier) {
    for (var property in supplier) {
        if (supplier.hasOwnProperty(property)) {
            receiver[property] = supplier[property];
        }
    }
}

To test this function, I am using the ok method of Qunit -

test('Mixin Test Runner', function () {
    // using OK method
    ok(function () {
        var receiver = {
            name: 'Abul Moksud',
            age: '60'
        };

        var supplier = {
            hands: 2
        };

        mixin(receiver, supplier);

        return receiver.hasOwnProperty('hands') && (receiver.hands === 2);
    }(), "Abul Moksud now has two hands");
});

I am an absolute beginner in both unit testing and QUnit. Is my approach OK here, or is there any better option?

\$\endgroup\$
2
\$\begingroup\$

Your code is correct, but can be simplified. ok method usually gets an expression rather than a in-place function that is immediately evaluated.

Here is how I would write this test:

test('Mixin Test Runner', function () {
    var receiver = {
        name: 'Abul Moksud',
        age: '60'
    };

    var supplier = {
        hands: 2
    };

    mixin(receiver, supplier);

    //Assertion phase
    ok(receiver.hasOwnProperty("hands"), "hands property is defined on reciever");
    equal(receiver.hands, 2, "Abul Moksud now has two hands");
});
| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.