Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

up vote 2 down vote accepted

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");
});
share|improve this answer
    
Nice, +1.......... –  Sayem Ahmed Dec 18 '12 at 17:50

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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