2013-04-28 40 views
0

使用這個類:如何測試獨立使用的方法?

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

運行這些測試(與摩卡)將通過他們的不過他們一起測試。

  1. 這是測試此代碼的正確方法,使用所涉及的流程中的所有方法?

  2. 如何獨立測試這些方法?

    2.1。我無法看到我測試的結構,因爲我只是測試實現,而不是接口。

    2.2。那麼,.on(ev, fn)的標準是成功的嗎?

回答

1

如果您之前沒有打開「打開」,您的關閉功能是否按預期運行?如果你的測試框架支持它,你也可以使用mock。