如何测试要单独一起使用的方法?
How do I test methods that are to be used together in isolation?
使用这个类:
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)将通过所有测试,但是它们一起测试。
这是测试这段代码的正确方法,使用所有的方法,在一个流程(s) ?
如何单独测试这些方法?
2.1。我不能看到我正在测试的结构的内部,因为我只是在测试实现,而不是接口。
2.2。那么,
.on(ev, fn)
成功的标准是什么呢?
你的off函数行为如预期的,如果你不调用'on'之前?如果测试框架支持mock,也可以使用mock。
相关文章:
- javascript中是否有更标准化的方法来转换(序列化)非表单数据以与ajax一起使用
- 一种将Apache Thrift JS绑定与Backbone.JS一起使用的方法
- 将 JSDoc 与匿名对象和该对象的函数一起使用的正确方法
- 与 promise 一起使用时,异步瀑布不执行下一个回调方法
- 将 react-redux 与基于事件的第三方库一起使用的最佳方法是什么?
- 将FitText与change()方法一起使用
- 获得错误“;对象没有方法'jScrollPane'"与AngularJS一起使用时
- 如何将包含猫鼬函数的方法与控制流框架(例如异步或 Step)一起使用
- jquery:如何将 .hide() 方法与 .fadeToggle(“fast”) 动画一起使用
- 错误 405 方法未分配异常,当使用 AJAX 与 Laravel 一起发布时
- 将闭包编译器的类型安全性与AMD(requirejs)结合在一起的最可靠方法是什么?
- 我可以将javascript和方法中的属性链接在一起吗?
- 是否可以在 javascript 中将对象的默认方法与其他方法一起使用
- 任何使postMessage与HTTPS一起工作的方法
- 为什么javascript方法在与setTimeout一起使用时会失去与对象的连接
- 与compareFunction一起使用的排序方法背后的逻辑是什么
- 有没有一种方法可以将json文件与所有其他js文件一起加载
- 如何在 JavaScript 中使用继承与构造函数方法一起使用 返回具有私有属性的对象文本
- MVC-有没有一种很好的方法可以将控件与它们各自的javascript捆绑在一起
- 将Ember.js与Fabric.js这样的画布库集成在一起的正确方法是什么