5

I am currently researching on how to efficiently add some unit tests to my app's ViewControllers. So far it worked pretty well until I tried to that that a specific view controller presents another one.

I am using OCMock and XCTest. The test is as follows

id partialMock = OCMPartialMock([TestViewController class]);
[partialMock doSomeStuff];
OCMVerify([partialMock presentViewController:[OCMArg any] animated:[OCMArg any] completion:[OCMArg any]]);

As you can see, I only want to verify that presentViewController was called to the tested view controller inside doSomeStuff function. Please note that the given example is a simplified version of what I currently have. Main difference being that I am verifying that the argument viewController is another mocked object.

Problem is since doSomeStuff method is not stubbed, the call is then forwarded to the real TestViewController instance, which then calls presentViewController on itself, then not firing the partialMock's verification.

Is there something I am missing? Or is it truly undoable what I am trying to achieve?

  • Hi Marc, I may be wrong here, but you are setting a partial mock on a class object. Usually, to set a regular mock object you use the class, but for a partial one, you should be setting it from the object you want to "spy", in this case, your viewController – J.C. Chaparro Nov 18 '15 at 15:36
  • Oh you are right, in fact my simplified example is not quite right. Because I have in fact two view controllers, but this makes me think of something. – Marc-Alexandre Bérubé Nov 18 '15 at 15:41

1 Answers1

1

You can stub the method you want to supress by using andDo(nil) as described in 2.10: http://ocmock.org/reference/

Erik Doernenburg
  • 2,933
  • 18
  • 21
  • is there a way I could stub that method on every instance of the class? My feeling is no, and since the method is invoked from the TestViewController class itself, it does use the stubbed method. I am not sure I am 100% clear on what my problem is – Marc-Alexandre Bérubé Nov 17 '15 at 17:30
  • It works.... I don't know what was wrong earlier, I think it was related to the animated:[OCMArg any] that was making my verification to fail – Marc-Alexandre Bérubé Nov 18 '15 at 16:05