在unit testing中,如何testing新缓冲区中创build的值?
假设我们有一些这样的代码:
callFunction(new Buffer(foo));
我们想用sinon.js间谍unit testing这个调用:
var spy = sinon.spy(callFunction); expect(spy.to.have.been.calledWith(foo));
这将失败,因为callFunction
是用new Buffer(foo)
而不是foo
调用的。 如果我们expect(spy.to.have.been.calledWith(new Buffer(foo))
testing通过expect(spy.to.have.been.calledWith(new Buffer(foo))
但为每个testing创build新的缓冲区是否明智?有没有更好的方法来做到这一点?
那么像CoffeeScript这样的东西呢?
bufferEqual = (expectation) -> sinon.match.instanceOf(Buffer) .and(sinon.match (val) -> val.toString() == expectation)
或JS版本:
function bufferEqual(expectation) { return sinon.match.instanceOf(Buffer).and(sinon.match(function(val) { return val.toString() === expectation; })); };