Created
February 14, 2020 12:48
-
-
Save tehvicke/6e30a93759c7298ca74b65955e921f83 to your computer and use it in GitHub Desktop.
Testing a service with mock models and a stub
This file contains 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
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