Node.js sinon在并行执行中存根函数会导致测试失败
我有两个测试用例,它们测试同一个函数,只是采用了两种不同的执行路径,以说明:Node.js sinon在并行执行中存根函数会导致测试失败,node.js,unit-testing,testing,mocha.js,sinon,Node.js,Unit Testing,Testing,Mocha.js,Sinon,我有两个测试用例,它们测试同一个函数,只是采用了两种不同的执行路径,以说明: MyClass.prototype.functionBeingTested = function() { if (this.check1()) { this.isCheck1Called = true; } else if (this.check2()) { this.isCheck1Called = false; } else { ...
MyClass.prototype.functionBeingTested = function() {
if (this.check1()) {
this.isCheck1Called = true;
} else if (this.check2()) {
this.isCheck1Called = false;
} else {
...
}
};
我的两个测试用例如下所示:
it('should take check1() execution path', function() {
var myClass= new MyClass({}, {}, {});
var check1Stub sinon.stub(MyClass.prototype, 'check1');
check1Stub.returns(true);
myClass.functionBeingTested();
myClass.isCheck1Called.should.equal(true);
});
it('should take check2() execution path', function() {
var myClass= new MyClass({}, {}, {});
var check2Stub sinon.stub(MyClass.prototype, 'check2');
check2Stub.returns(true);
myClass.functionBeingTested();
myClass.isCheck1Called.should.equal(false);
});
现在,默认情况下,check1()
返回false,因此我不会在第二个测试用例中存根,但在第二个用例运行时,check1()
函数存根仍然处于活动状态,并导致第二个用例也进入第一个用例的执行路径,从而导致第二个用例测试失败
我知道这是一个并行运行的测试问题,第一个测试用例仍在使用第一个sinon存根,我是否可以解决这个问题?在第一个测试结束时,您应该恢复原始方法(这始终是一件好事,以防止测试受到以前测试的影响): 或者,您也可以使用Sinon在以下位置运行每个测试:
describe('MyClass', function() {
beforeEach(function() {
this.sinon = sinon.sandbox.create();
});
afterEach(function() {
this.sinon.restore();
});
it('should take check1() execution path', function() {
var myClass = new MyClass({}, {}, {});
// `this.sinon` is the sandbox
var check1Stub = this.sinon.stub(MyClass.prototype, 'check1');
check1Stub.returns(true);
myClass.functionBeingTested();
myClass.isCheck1Called.should.equal(true);
});
it('should take check2() execution path', function() {
var myClass = new MyClass({}, {}, {});
var check2Stub = this.sinon.stub(MyClass.prototype, 'check2');
check2Stub.returns(true);
myClass.functionBeingTested();
myClass.isCheck1Called.should.equal(false);
});
});
(请参阅,其功能完全相同)太棒了,感谢您向我展示摩卡西农替代品,非常好的解决方案。我使用它时有点问题。我用
require('mocha-sinon')创建了一个spec_helper.js文件代码>在它,我需要它在我的测试文件中,我使用间谍和存根,但它仍然失败,我看到的文档和示例与我所做的匹配。你能根据我所拥有的更新你的答案以包括设置吗?这样的设置对我来说很好,只要确保你在测试用例中使用this.sinon
而不是sinon
。它可能取决于调用require('./spec\u helper')
的位置,只需将其移动到descripe()
块之外的某个位置即可。
describe('MyClass', function() {
beforeEach(function() {
this.sinon = sinon.sandbox.create();
});
afterEach(function() {
this.sinon.restore();
});
it('should take check1() execution path', function() {
var myClass = new MyClass({}, {}, {});
// `this.sinon` is the sandbox
var check1Stub = this.sinon.stub(MyClass.prototype, 'check1');
check1Stub.returns(true);
myClass.functionBeingTested();
myClass.isCheck1Called.should.equal(true);
});
it('should take check2() execution path', function() {
var myClass = new MyClass({}, {}, {});
var check2Stub = this.sinon.stub(MyClass.prototype, 'check2');
check2Stub.returns(true);
myClass.functionBeingTested();
myClass.isCheck1Called.should.equal(false);
});
});