Node.js sinon 在并行执行中存根函数导致测试失败
Node.js sinon stubbing a function in parallel executions causes failed tests
我有 2 个测试用例,它们测试同一个函数,只是采用 2 个不同的执行路径,所以为了说明:
MyClass.prototype.functionBeingTested = function() {
if (this.check1()) {
this.isCheck1Called = true;
} else if (this.check2()) {
this.isCheck1Called = false;
} else {
...
}
};
我的2个测试用例如下:
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()
returns false 所以我不在第二个测试用例中存根,但是当第二个用例是 运行 时,check1()
函数存根仍然处于活动状态,并导致第二个案例也进入第一个案例的执行路径,从而使第二个案例测试失败。
我知道这是并行测试 运行 的问题,第一个测试用例仍在使用第一个 sinon 存根,无论如何我可以解决这个问题吗?
在第一次测试结束时,你应该恢复原来的方法(这总是一件好事,以避免测试被以前的测试影响):
check1Stub.restore()
或者,您可以使用 Sinon sandbox 来 运行 每个测试:
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);
});
});
(参见 mocha-sinon
,其作用完全相同)
我有 2 个测试用例,它们测试同一个函数,只是采用 2 个不同的执行路径,所以为了说明:
MyClass.prototype.functionBeingTested = function() {
if (this.check1()) {
this.isCheck1Called = true;
} else if (this.check2()) {
this.isCheck1Called = false;
} else {
...
}
};
我的2个测试用例如下:
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()
returns false 所以我不在第二个测试用例中存根,但是当第二个用例是 运行 时,check1()
函数存根仍然处于活动状态,并导致第二个案例也进入第一个案例的执行路径,从而使第二个案例测试失败。
我知道这是并行测试 运行 的问题,第一个测试用例仍在使用第一个 sinon 存根,无论如何我可以解决这个问题吗?
在第一次测试结束时,你应该恢复原来的方法(这总是一件好事,以避免测试被以前的测试影响):
check1Stub.restore()
或者,您可以使用 Sinon sandbox 来 运行 每个测试:
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);
});
});
(参见 mocha-sinon
,其作用完全相同)