简体   繁体   中英

Testing method signatures in Node.js

I'm relatively new to Unit-testing and TDD specificly and am about to start my first project with TDD using mocha and chai.

Am I supposed to test the existence and parameter length of the methods? And if so, is there any better way of doing it than I currently am? It feels extremly verbose, especially when repeating this for most of my classes.

For understand I've set up some dummy test.

test/index.js

'use strict';

const assert = require('chai').assert;

const Test = require('../lib/index.js');

describe('Test', function() {
    it('should be a function without parameters', function() {
        assert.isFunction(Test);
        assert.lengthOf(Test, 0);
    });

    let test;
    beforeEach(function() {
        test = new Test();
    });

    describe('static#method1', function() {
        it('should have static method method1 with 1 parameter', function() {
            assert.property(Test, 'method1');
            assert.isFunction(Test.method1);
            assert.lengthOf(Test.method1, 1);
        });
        it('should assert on non-string parameters', function() {
            const params = [
              123,
              {},
              [],
              function() {}
            ];
            params.forEach(function(param) {
                assert.throws(function() {
                    Test.method1(param)
                });
            });
        });
        it('should return "some value"', function() {
            assert.equal(Test.method1('param'), 'some value')
        });
    });
    describe('method2', function() {
        it('should have method method2 with 2 parameters', function() {
            assert.property(test, 'method2');
            assert.isFunction(test.method2);
            assert.lengthOf(test.method2, 2);
        });
        it('should assert on non-number parameters', function() {
            const params = [
                'some string',
                {},
                [],
                function() {}
            ];
            params.forEach(function(param) {
                assert.throws(function() {
                    test.method2(param)
                });
            });
        });
        it('should add the parameters', function() {
            assert.equal(test.method2(1, 2), 3);
            assert.equal(test.method2(9, -2), 7);
            assert.equal(test.method2(3, -12), -9);
            assert.equal(test.method2(-7, -5), -12);
        })
    });
});

And the tested implementation.

lib/index.js

'use strict';

const assert = require('chai').assert;

exports = module.exports = (function() {
    class Test {
        static method1(param0) {
            assert.typeOf(param0, 'string');
            return 'some value';
        }

        method2(param0, param1) {
            assert.typeOf(param0, 'number');
            assert.typeOf(param1, 'number');
            return param0 + param1;
        }
    }

    return Test;
}());

No, such detailed tests are not necessary. What is the value of them? What do they help you to achieve?

Usually when testing functions we test behavior of a function, not its implementation. Implementation can completely change without changing the observable behavior: for example, you can find more readable way to rewrite your code or a more performant algorithm.

You test the call signature of your function by the whole set of tests for it, indirectly. Every test provides an example of how to use your function , thus ensuring its call signature and return parameters.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM