I'm testing a representation of a NoSQL document in Jasmine. Each document has a save
method, which saves the changes to the object to the database, and a reload
method, which loads the document from the database into the object. I'd like to define a sync
method, which simply calls save
and then calls reload
.
My test for this method looks like this (subject
is the document instance):
describe 'sync', ->
it 'saves and then reloads', ->
spyOn(subject, 'save')
spyOn(subject, 'reload')
subject.sync()
expect(subject.save).toHaveBeenCalled()
expect(subject.reload).toHaveBeenCalled()
However, this test is incomplete; it passes for a sync
method that reloads before saving, which would discard changes made to the document.
How can I assert that the save method is called before the reload method?
One solution I found was to push onto an array instead of using Jasmine spies:
describe 'sync', ->
it 'saves and then reloads', ->
callOrder = []
subject.save = -> callOrder.push 'save'
subject.reload = -> callOrder.push 'reload'
subject.sync()
expect(callOrder).toEqual(['save', 'reload'])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With