Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jasmine assert call order

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?

like image 835
Suchipi Avatar asked Oct 25 '15 00:10

Suchipi


1 Answers

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'])
like image 99
Suchipi Avatar answered Sep 21 '22 17:09

Suchipi