node.js - 在 Node.js 中测试方法签名

标签 node.js unit-testing tdd mocha.js chai

我对单元测试和 TDD 还比较陌生,即将使用 mocha 和 chai 开始我的第一个 TDD 项目。

我应该测试方法的存在性和参数长度吗? 如果是这样,有没有比我现在更好的方法?感觉非常冗长,尤其是在我的大多数类(class)中重复这一点时。

为了理解,我设置了一些虚拟测试。

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);
        })
    });
});

以及经过测试的实现。

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;
}());

最佳答案

不,没有必要进行如此详细的测试。它们的值(value)是什么?它们能帮助您实现什么目标?

通常,在测试函数时,我们测试函数的行为,而不是其实现。实现可以完全改变,而不改变可观察的行为:例如,您可以找到更可读的方式来重写代码或更高性能的算法。

您可以通过整套测试来间接测试函数的调用签名。每个测试都提供了如何使用函数的示例,从而确保其调用签名和返回参数。

关于node.js - 在 Node.js 中测试方法签名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36721038/

相关文章:

Ruby 的 vcr 的 Java 替代品?

javascript - 有没有办法重组node_modules?

javascript - 将 gulp-file 分成几个文件

c# - 从 Main 方法/在 MonoDevelop 中运行 XUnit.Net 测试

java - 如何测试 "Too Many Files Open"问题

ruby-on-rails - 使用 rspec 进行测试 .consider_all_requests_local = false

mysql - 我应该如何将 LoopBack Framework 应用程序连接到预先存在的/数据填充的 MySQL 数据库/数据源?

javascript - 来自本地主机 :3000/admin to localhost:3000

java - 在测试期间清除嵌入式 activemq 数据

Scala 和 Akka - 使用 Akka Testkit 将 actor 作为系统进行测试