0

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);
}
1
  • 3
    can you post a sample of the type of code you're trying to test? Commented Oct 27, 2009 at 5:33

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

@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).
2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.