OCMock - checking the order of method calls. Is there any other way?

When I want to check that in one method the layout object receives some messages in a specific order, I do something like this:

// sut is an instance of the class I am testing and myMock is a mock object injected in sut. // I want to test that myMock sends messageA and then messageB, in that particular order. [[[myMock expect] andDo:^(NSInvocation *invocation) { [[myMock expect] messageB]; }] messageA]; [sut methodToTest]; [myMock verify]; 

Is there a cleaner / better way to do this? Thanks in advance.

+6
source share
2 answers

You can use setExpectationOrderMatters

 [myMock setExpectationOrderMatters:YES]; [[myMock expect] messageA]; [[myMock expect] messageB]; [sut methodToTest]; [myMock verify]; 
+15
source

It looks pretty clean for me. If you are not happy with nesting, you can enter a block variable.

 __block BOOL hasCalledA; [[[myMock expect] andDo:^(NSInvocation *invocation) { hasCalledA = YES; }] messageA]; [[[myMock expect] andDo:^(NSInvocation *invocation) { STAssertTrue(hasCalledA); }] messageB]; 

The solution looks great.

As a side note, I think this question might be better suited for https://codereview.stackexchange.com/ , although I'm still hugging around this site.

+2
source

Source: https://habr.com/ru/post/943553/


All Articles