Last active
January 12, 2020 19:09
-
-
Save Farenheith/e9b92a663c1a8699185b1a423ce58fba to your computer and use it in GitHub Desktop.
An example of test suite
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { stub, SinonStub } from 'sinon'; | |
// First describe, class name | |
describe('MyClass', () => { | |
let service: MyService; | |
let target: MyClass; | |
// Class describe beforeEach, class bootstrap | |
beforeEach(() => { | |
service = {} as any; | |
target = new MyClass(service); | |
}); | |
//method describe, wrapping all test cases for one method | |
describe('.myClassMethod()', () => { | |
let serviceMethod: SinonStub; | |
let anotherClassMethod: SinonStub; | |
// Method describe beforeEach, stubs bootstrap, where all the methods that could be called are stubbeds | |
beforeEach(() => { | |
serviceMethod = service.serviceMethod = stub(); | |
anotherClassMethod = stub(target, 'anotherClassMethod').returns('anotherClassMethod result'); | |
}); | |
// A single test case, where all stubbed methods are called | |
it('should do something when something happens', async () => { | |
serviceMethod.returns('valid value for this test case'); | |
const result = await target.myClassMethod(); | |
expect(serviceMethod).to.have.been.calledOnceWithExatly('some value'); | |
expect(anotherClassMethod).to.have.been.calledOnceWithEactly('valid value for this test case'); | |
expect(result).to.be.eq('anotherClassMethod result'); | |
}); | |
// Another test case, where one of the stubbed methods are not called | |
it('should do another thing when something happens', async () => { | |
serviceMethod.returns('another value, valid for this test case'); | |
const result = await target.myClassMethod(); | |
expect(serviceMethod).to.have.been.calledOnceWithExatly('some value'); | |
expect(anotherClassMethod).to.have;not.been.called; | |
expect(result).to.be.eq('anotherClassMethod result'); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment