erikdoe / ocmock

Mock objects for Objective-C
http://ocmock.org
Apache License 2.0
2.16k stars 606 forks source link

Introduce OCMAccept to revert an earlier OCMReject call? #361

Closed calimarkus closed 4 years ago

calimarkus commented 6 years ago

In a test I wrote I want to proof a certain method isn't being called for the first half of the test, but expect it for the latter part. One solution to support that would be to disallow the call, when I don't expect it, but then later allow it again. Another alternative solution would be smth. like OCMVerifyCount(invocation, expectedTotalMethodInvocationCount) similar to OCMockito, where we ensure the method was only called once total (assuming from the right callsite).

Am I missing something or is that currently not possible?

Envisioned example:

- (void)testSomething {
  id someMock = OCMClassMock([SomeClass mock]);

  SomeClassWithDeps *actingClass = [[SomeClassWithDeps alloc] initWithDep:someMock];

  OCMReject([someMock callbackMethod]);
  [actingClass someMethodThatShouldNotTriggerTheCallback];

  OCMAllow([someMock callbackMethod]);
  [actingClass someMethodThatDoesTriggerTheCallback];

  OCMVerify([someMock callbackMethod]);
}

Slightly related: Would it be possible to trigger a test failure on the correct line, instead of throwing an exception, if a method is called, that was rejected before?

erikdoe commented 4 years ago

I'll work on #302 in the next couple of weeks. That should fix this issue.