Skip to content

Instantly share code, notes, and snippets.

@tehvicke
Created February 14, 2020 12:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tehvicke/6e30a93759c7298ca74b65955e921f83 to your computer and use it in GitHub Desktop.
Save tehvicke/6e30a93759c7298ca74b65955e921f83 to your computer and use it in GitHub Desktop.
Testing a service with mock models and a stub
describe('listThoughts test', () => {
it('should list Thoughts', () => {
/* Mock model to imitate the find function */
const MockModel = {
find: () => {}
}
/* We create a stub to imitate chained methods */
sinon.stub(MockModel, 'find').callsFake(() => {
return {
sort: () => {
return {
limit: sinon.stub().returns()
}
}
}
})
const thoughtService = ThoughtService(MockModel)
thoughtService.listThoughts()
const expected = true
const actual = MockModel.find.calledOnce
expect(actual).toEqual(expected)
})
}),
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment