简体   繁体   中英

How to check if a method is called before another one got called

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 ?

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));
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM