gomockingtestify

Verify order of mocks call


I use testify (v1.6.1) and need to test if methods of interfaces call in right order. I checked the documentation and tried to find any information in the internet, but didn't find anything about mocks order checking.

Example:

type InterfaceA interface {
    Execute()
}

type InterfaceB interface {
    Execute()
}
type Composition struct {
    a InterfaceA
    b InterfaceB
}

func (c * Composition) Apply() error {
    //How to check that "a" execute before "b"?
    c.a.Execute()
    c.b.Execute()
    return nil
}

Solution

  • This is not directly supported, even though there is an opened issue (stretchr/testify/issue 741 "assert mock calls in order")

    The more general issue 684 "Assert call order?" includes the rebuttal:

    IMO you should check the output of the function but not how it works internally. It may lead to testing implementation which is very hard to maintain.

    Although, in the same thread:

    IMO there are cases to enforce order. i.e if you mock a mutex, you better check that Lock was always called before Unlock.
    We can have a simple implementation where the mock has an "assertExpectationsInOrder" true/false flag that can be set before any expectations are added.

    This can lead to some test like cassandra-operator/cmd/operator/controller_test.go which records events to test their orders.