views:

75

answers:

2

Hi all:

As topic suggested, how can I unit test a function that does not return anything (apart from null checks that simply returns)?

And also, how do I unit test the functions within a function which also does not return anything especially if they are at the last lines of a function?

The reason behind this question is that I've been trying to use Jack and JsMockito to test my code, and they all required me to provide some sort of object which seems to be returned by a function in the code, hence I'm lost on how to do so.

Thanks.

EDIT:

Here is a sample pesudo code:

function myFunction()
{
    local var

    null check {
        return;
    }

    null check
    {
        return;
    }

    // points to another function
    function(param1);

    // points to a C# function
    function(param1, param2);
}
+3  A: 

Surely your function must have some effect or change to the system/program, that can be detected/compared outside the function.

If it does nothing => delete the code, you don't need it!

If it is meant to throw errors, try to catch them (make calls containing know wrong parameters/conditions, then see if they are caught.

lexu
@lexu: I don't mean dead code. Coming from a Java background, I cannot see the Javascript functions actually return anything, so I was having difficulties testing it (also trying to get use to the new syntax).
BeraCim
+1  A: 

You need to work out what the side effects of the function are, then test that they happen.

The code above calls two other functions. So your unit test would validate that they are called, most likely by mocking them in some way.

mlk