Testing Sinon监视所有方法调用
我试图测试某个特定的实例方法是否至少被调用过一次。使用Testing Sinon监视所有方法调用,testing,coffeescript,sinon,Testing,Coffeescript,Sinon,我试图测试某个特定的实例方法是否至少被调用过一次。使用mocha和sinon。有两类:A和BB#render在A#render内部调用。无法访问测试文件中的B实例 sinon.spy B.prototype, 'render' @instanceA.render B.prototype.render.called.should.equal true B.prototype.render.restore() 这是正确的做法吗? 谢谢你你应该这样做: const chai = require('c
mocha
和sinon
。有两类:A
和B
B#render
在A#render
内部调用。无法访问测试文件中的B
实例
sinon.spy B.prototype, 'render'
@instanceA.render
B.prototype.render.called.should.equal true
B.prototype.render.restore()
这是正确的做法吗?
谢谢你你应该这样做:
const chai = require('chai');
const sinon = require('sinon');
const SinonChai = require('sinon-chai');
chai.use(SinonChai);
chai.should();
/**
* Class A
* @type {B}
*/
class A {
constructor(){
this.b = new B();
}
render(){
this.b.render();
}
}
/**
* Class B
* @type {[type]}
*/
class B {
constructor(){
}
render(){
}
}
context('test', function() {
beforeEach(() => {
if (!this.sandbox) {
this.sandbox = sinon.sandbox.create();
} else {
this.sandbox.restore();
}
});
it('should pass',
(done) => {
const a = new A();
const spyA = this.sandbox.spy(A.prototype, 'render');
const spyB = this.sandbox.spy(B.prototype, 'render');
a.render();
spyA.should.have.been.called;
spyB.should.have.been.called;
done();
});
});
你的假设是正确的。在类的原型级别添加间谍。希望有帮助:)你应该这样做:
const chai = require('chai');
const sinon = require('sinon');
const SinonChai = require('sinon-chai');
chai.use(SinonChai);
chai.should();
/**
* Class A
* @type {B}
*/
class A {
constructor(){
this.b = new B();
}
render(){
this.b.render();
}
}
/**
* Class B
* @type {[type]}
*/
class B {
constructor(){
}
render(){
}
}
context('test', function() {
beforeEach(() => {
if (!this.sandbox) {
this.sandbox = sinon.sandbox.create();
} else {
this.sandbox.restore();
}
});
it('should pass',
(done) => {
const a = new A();
const spyA = this.sandbox.spy(A.prototype, 'render');
const spyB = this.sandbox.spy(B.prototype, 'render');
a.render();
spyA.should.have.been.called;
spyB.should.have.been.called;
done();
});
});
你的假设是正确的。在类的原型级别添加间谍。希望有帮助:)