如何测试单独使用的方法?

时间:2013-04-28 00:18:28

标签: javascript testing

使用此课程:

var Observer = function Observer() {
    this._events = {};
};

Observer.prototype.on = function(k, v) {
    (this._events[k] = this._events[k] || []).push(v);
};

Observer.prototype.off = function(k, v) {
    var hs;
    var i;
    var l;

    if (!k) {
        this._events = {};
        return;
    }

    if (!v) {
        if (typeof this._events[k] !== 'undefined') {
            this._events[k] = [];
        }
        return;
    }

    hs = this._events[k];
    i = 0;
    l = hs.length;
    while(i < l) {
        if (hs[i] === v) {
            hs.splice(i);
        }
        i++;
    }
};

Observer.prototype.emit = function(k) {
    var hs = this._events[k];
    var args;
    var i;
    var l;

    if (hs) {
        args = [].splice.call(arguments, 1);
        i = 0;
        l = hs.length;

        while(i < l) {
            hs[i].apply(this, args);
            i++;
        }
    }
};

if (typeof exports !== 'undefined') {
    exports.Observer = Observer;
}

和这个测试代码:

var assert = require('assert');
var Observer = require('../../public/lib/Observer').Observer;

describe('Observer', function() {
    beforeEach(function(done) {
        this.instance = new Observer();
        done();
    });

    describe('#on', function() {
        describe('with an event and handler', function() {
            it('should call a callback', function(done) {
                var expected = 0;

                this.instance.on('a', function(actual) {
                    assert.equal(expected, actual);
                    done();
                });

                this.instance.emit('a', expected);
            });
        });
    });

    describe('#off', function() {
        describe('with an event and a handler', function() {
            it('should not call the callback', function(done) {
                var expected = false;
                var actual = false;

                var f = function() {
                    actual = true;
                };

                this.instance.on('a', f);

                this.instance.off('a', f);

                this.instance.emit('a');

                assert.equal(expected, actual);

                done()
            });
        });

        describe('with an event', function() {
            it('should not call the callback', function(done) {
                var expected = false;
                var actual = false;

                var f = function() {
                    actual = true;
                };

                this.instance.on('a', f);

                this.instance.off('a');

                this.instance.emit('a');

                assert.equal(expected, actual);

                done()
            });
        });

        describe('without arguments', function() {
            it('should not call the callback', function(done) {
                var expected = false;
                var actual = false;

                var f = function() {
                    actual = true;
                };

                this.instance.on('a', f);

                this.instance.off('a');

                this.instance.emit('a');

                assert.equal(expected, actual);

                done()
            });
        });
    });

    describe('#emit', function() {
        describe('with an event and variable arguments', function() {
            // I've already used this method in the other tests, so it must work in
            // order for them to pass. But that's not isolated testing, help :(..
        });
    });
});

运行这些测试(使用mocha)会将它们全部传递,但它们会一起测试。

  1. 这是使用流程中涉及的所有方法测试此代码的正确方法吗?

  2. 如何单独测试这些方法?

    2.1。我无法查看我正在测试的结构,因为我只是测试实现,而不是接口。

    2.2。那么,.on(ev, fn)成功的标准是什么?

1 个答案:

答案 0 :(得分:0)

如果您之前没有打开'on',你的off功能是否会按预期运行?如果您的测试框架支持,您也可以使用模拟。