Search code examples
javascriptunit-testingqunit

How to handle inner functions in qunit


I just started with Qunit, and don't know much about it.

The problem I'm having is that I have written this function in my code and want to test this using Qunit.

  <script>
    function calc(firstno,secnum){
       return firstno + secnum;
     }

   function main (firstno,secnum){

    return  calc(firstno,secnum);

    }


 </script>

So, how should I test the calc function when I write a test case for the main function.


Solution

  • test('Testing that main adds numbers', function() {
        var a = 1;
        var b = 3;
    
        var result = main(a, b);
        equal(4, result, 'Main adds two numbers together');
    });
    

    When you are testing main(), you are not concerned that it calls calc() only that it returns two numbers added together.

    In your example, you would likely have a separate test for calc() as it is publicly available. And this way you are saying that you have functions main and calc.

    You are testing the behavior of the code rather than the actual implementation. The fact that main calls calc is not what you are trying to test. Only what the actual return of main is.