A_user
A_user

Reputation: 2157

Javascript unit testing of functions with no return type

I have just started working on JavaScript unit testing and I am new to it.

I have found many unit testing frameworks and I have decided to go with QUnit. But I want to ask that in unit testing most of the frameworks are like that we pass different values to function and then compare the return value with the expected value and then pass or fail the test. In my case I don't have all the functions returning the value.Some are changing class variables i.e vars array etc are changed in the same function.

So just want to ask that is there a way to check such type of functions or Is it must to have a return type?

If yes than should I have to know the expected value of every var or array before hand?

Thanks any help will be appreciated

Upvotes: 2

Views: 2914

Answers (1)

rodrigo-silveira
rodrigo-silveira

Reputation: 13086

If the function is just modifying member data, you can just query the data before you call the function, then query that data again after you call the function. That way you can determine if the function is in fact doing what it should, the way it should.

Pseudo example:

var MyClass = function(data) {

    // Suppose data is an array
    this.data = data;


    this.sort = function() {
        // sorts data
    };

};

// Test #1:
var myClass = new MyClass([5, 3, 2, 4, 1]);
myClass.sort();

var expected = [1, 2, 3, 4, 5];
deepEqual(myClass.data, expected);

Upvotes: 4

Related Questions