0

I have a method called mainMethod() and it returns a promise. This method contains several methods m1(), m2()...,m5(). Now I am making a unit test using sinon,

I want to check if m1() is called and m2() is not called.

Because I have an array that not empty after m1() is called but it will be empty after m2() is called.

I want to make a check or test after m1() is called and before m2() is called.

Is it possible using sinon?

Dennis Vash
  • 50,196
  • 9
  • 100
  • 118
Amrmsmb
  • 1
  • 27
  • 104
  • 226

1 Answers1

1

Yes, using Sinon is possible. It has calledAfter and calledBefore.

http://sinonjs.org/releases/v6.1.3/spies/

For example

it('some testing', function() {
    var m1 = { method: function () {} };
    var m2 = { method: function () {} };

    var spyM1 = sinon.spy(m1, "method");
    var spyM2 = sinon.spy(m2, "method");

    m1.method(42);
    m2.method(1);

    assert(spyM1.calledBefore(spyM2));
    assert(spyM2.calledAfter(spyM1));
});
deerawan
  • 8,002
  • 5
  • 42
  • 51