21 global - ranhs/soda-test GitHub Wiki
When you define spy, stub, rewire or fakeTimer on context or class scope, it is recreated and releases by default for each test-step. You can use the global decorator to mark this sinon to be created and release once for the whole scope.
You must define the global decorator before the sinon decorator
@global()
@stub(testObject, 'func2').returns(222)
func2Stub2: SinonStub
class TestObjectClass {
func1(): number {
return 1
}
func2(): number {
return 2
}
func3(): number {
return 3
}
}
const testObject = new TestObjectClass()
@spy(testObject,'func1')
func1Spy1: SinonSpy
@stub(testObject, 'func2').returns(22)
func2Stub1: SinonStub
@it('should called once')
calledOnce1(): TR {
expect(this.func1Spy1).to.not.have.been.called
expect(this.func2Stub1).to.not.have.been.called
expect(testObject.func1()).to.equal(1)
expect(testObject.func2()).to.equal(22)
expect(testObject.func3()).to.equal(3)
expect(this.func1Spy1).to.have.been.calledOnce
expect(this.func2Stub1).to.have.been.calledOnce
}
@it('should called once again')
calledOnce2(): TR {
expect(this.func1Spy1).to.not.have.been.called
expect(this.func2Stub1).to.not.have.been.called
expect(testObject.func1()).to.equal(1)
expect(testObject.func2()).to.equal(22)
expect(testObject.func3()).to.equal(3)
expect(this.func1Spy1).to.have.been.calledOnce
expect(this.func2Stub1).to.have.been.calledOnce
}
In test step 'should called once' we have called all functions one time, and validated they have been called once. In test step 'should called once again' we called again all functions and they are validated to be called only once time each, since we used regular sinons, the spy and the stub was recreated for each test-step, and count only the calls from the current test-step
@global()
@spy(testObject,'func1')
func1Spy2: SinonSpy
@global()
@stub(testObject, 'func2').returns(222)
func2Stub2: SinonStub
@it('should called once')
calledOnce3(): TR {
expect(this.func1Spy2).to.not.have.been.called
expect(this.func2Stub2).to.not.have.been.called
expect(testObject.func1()).to.equal(1)
expect(testObject.func2()).to.equal(222)
expect(testObject.func3()).to.equal(3)
expect(this.func1Spy2).to.have.been.calledOnce
expect(this.func2Stub2).to.have.been.calledOnce
}
@it('should called once again')
calledOnce4(): TR {
expect(this.func1Spy2).to.have.been.calledOnce
expect(this.func2Stub2).to.have.been.calledOnce
expect(testObject.func1()).to.equal(1)
expect(testObject.func2()).to.equal(222)
expect(testObject.func3()).to.equal(3)
expect(this.func1Spy2).to.have.been.calledTwice
expect(this.func2Stub2).to.have.been.calledTwice
}
In test step 'should called once' we have called all functions one time, and validated they have been called once. In test step 'should called once again' we called again all functions and they are validated to be called only 2 times each, since we used global sinons, the spy and the stub "remember" the calls from prevous test-step, and count total number of calls