Is there a way to verify the order of spy executions with Jasmine?

不羁的心 提交于 2019-12-03 23:40:19

Looks like the Jasmine folks saw this post or others like it, because this functionality exists. I'm not sure how long it's been around -- all of their API docs back to 2.6 mention it, though none of their archived older style docs mention it.

toHaveBeenCalledBefore(expected)
expect the actual value (a Spy) to have been called before another Spy.

Parameters:

Name        Type    Description
expected    Spy     Spy that should have been called after the actual Spy.

A failure for your example looks like Expected spy spy1 to have been called before spy spy2.

So far I've been doing it as follows, but it seems awkward and won't scale well:

obj.spy1.andCallFake(function() {
    expect(obj.spy2.calls.length).toBe(0);
});

Another alternative is to keep a list of calls:

var objCallOrder;
beforeEach(function() {
  // Reset the list before each test
  objCallOrder = [];
  // Append the method name to the call list
  obj.spy1.and.callFake(function() { objCallOrder.push('spy1'); });
  obj.spy2.and.callFake(function() { objCallOrder.push('spy2'); });
});

Which lets you check the order in a few different ways:

Directly compare to the call list:

it('calls exactly spy1 then spy2', function() {
  obj.spy1();
  obj.spy2();

  expect(objCallOrder).toEqual(['spy1', 'spy2']);
});

Check the relative order of a few of the calls:

it('calls spy2 sometime after calling spy1', function() {
  obj.spy1();
  obj.spy3();
  obj.spy4(); 
  obj.spy2();

  expect(obj.spy1).toHaveBeenCalled();
  expect(obj.spy2).toHaveBeenCalled();
  expect(objCallOrder.indexOf('spy1')).toBeLessThan(objCallOrder.indexOf('spy2'));
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!