0

I have a XCTest case with the following code:

#import <XCTest/XCTest.h>
#import <OCMock/OCMock.h>

@interface ChildViewControllerTests : XCTestCase
@end

@implementation ChildViewControllerTests

-(void) testInitAddsPassedControllersAsChildViews {
    id mockTVC1 = [OCMockObject niceMockForClass:[UITableViewController class]];
    id mockTVC2 = [OCMockObject niceMockForClass:[UITableViewController class]];
    UIViewController *controller = [[UIViewController alloc] init];
    [controller addChildViewController:mockTVC2]; // Fails if mockTVC2, but mockTVC1 works.
}

@end

It's as simple as I can make it. When I run this test, I get an EXEC_BAD_ACCESS deallocing mockTVC2.

If I add mockTVC1 to the controller instead, it works just fine.

If I swap the order of the mockTVC1 and mockTVC2 declarations, it works just fine.

If I swap the order of the mockTVC1 and mockTVC2 declarations and change to adding mockTVC1, it fails again de-allocing.

So my summation is that the problem has something to do with the declaring of the second mock because no matter how I order the declarations, if I attempt to add the mock declared second to the controller, then it fails when de-allocing.

Can some of you guys do a quick cut-n-paste and see if you get this to occur as well?

I'd like to confirm it's not just my setup.

Thanks

drekka
  • 20,957
  • 14
  • 79
  • 135
  • 1
    Have tried your code in my machine and it seems to fail randomly with both mockTVC1 and mockTVC2(iOS 7 simulator)... I would better try to use real table view controllers instead(if you can). I do that sometimes instead of mocking UIKit classes like UITextField, etc. – e1985 Oct 07 '13 at 09:12
  • Thanks for checking that out. I was using mocks because I wanted to check out the functionality of a customer controller. The original code was using mocks of class extending UIViewController. The above example is probably best done with real UIViewControllers as you say, but I'm interested in what the problem could be when using OCMock. – drekka Oct 07 '13 at 09:55
  • What are you trying to test? You might get this to work by using a partial mock. – Ben Flynn Oct 07 '13 at 15:51

0 Answers0