Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I test methods that are to be used together in isolation?
    primarykey
    data
    text
    <p>Using this class:</p> <pre><code>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 &lt; 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 &lt; l) { hs[i].apply(this, args); i++; } } }; if (typeof exports !== 'undefined') { exports.Observer = Observer; } </code></pre> <p>and this test code:</p> <pre><code>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 :(.. }); }); }); </code></pre> <p>Running these tests (with mocha) will pass them all however they are tested together.</p> <ol> <li><p>Is this the correct way of testing this code, using all the methods involved in the flow(s) of one?</p></li> <li><p>How do I test these methods in isolation?</p> <p>2.1. I can't look inside the structure of what I'm testing, because I'd just be testing the implementation, not the interface.</p> <p>2.2. What then, are the criteria for just <code>.on(ev, fn)</code> being successful?</p></li> </ol>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload