【发布时间】:2015-11-20 08:59:41
【问题描述】:
sinon.spy(object, method) 似乎没有按预期包装我的 object#method。
(我有一种不安的感觉,我看到了与 here 和 here 描述的相同的问题,但我不明白为什么会这样。我在调用 @ 之前实例化了我的对象987654324@ 和 AFAIK,我没有使用任何缓存对象。)
这是完整的测试文件:
var
AmbitParser = require('../lib/parsers/ambit-parser'),
expect = require('chai').expect,
sinon = require('sinon');
describe('AmbitParser', function() {
var ambit_parser = new AmbitParser();
describe('#extractLineItems()', function() {
it('calls extractLineItems once', function(done) {
var spy = sinon.spy(ambit_parser, 'extractLineItems');
ambit_parser.parseBills(function gotBills(err, bills) {
expect(ambit_parser.extractLineItems.callCount).to.equal(1); // => expected undefined to equal 1
expect(spy.callCount).to.equal(1); // => expected 0 to equal 1
done();
});
ambit_parser.extractLineItems.restore();
}); // calls extractLineItems once
}); // #extractLineItems
}); // AmbitParser
对expect(ambit_parser.extractLineItems.callCount).to.equal(1); 的调用导致“预期未定义等于1”,如果我将其更改为expect(spy.callCount).to.equal(1);,我会得到“预期0 等于1”。
总的来说,这让我认为对 sinon.spy(...) 的调用没有按预期包装 ambit_parser.extractLineItems 方法,但我不明白为什么会这样。
【问题讨论】:
标签: javascript mocha.js sinon