I'm new to OCMock and i'm trying to test if a method is called, except I want the method to be a stub since it contains other loading methods I do not care to test in the current test.

- (void)setUp
{
    self.rootViewController = [[RootViewController alloc] init];
}

- (void)tearDown
{
   self.rootViewController = nil;
}

- (void)test_ViewDidLoad_DidCallLoadChildViewControllers
{
    id mockRootViewController = [OCMockObject partialMockForObject:self.rootViewController];

    [(RootViewController*)[mockRootViewController stub] loadChildViewControllers];
    [(RootViewController*)[mockRootViewController stub] updateMainNavigation];

    [[mockRootViewController expect] loadChildViewControllers];

    // Force the view to load
    self.rootViewController.view;

    [mockRootViewController verify];
}

And here is the method that is getting called:

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self loadChildViewControllers];
    [self updateMainNavigation];
}

For some reason the test is failing as if it is never called (yet I can step thru it and verify that it is getting called, as well as it being a stub.

Here is the error:

OCPartialMockObject[RootViewController]: expected method was not invoked: loadChildViewControllers

What am I missing?

有帮助吗?

解决方案

Try removing this line:

[(RootViewController*)[mockRootViewController stub] loadChildViewControllers];

When you expect that method, it has the side effect of stubbing it, so the stub is unnecessary. I suspect what's happening is that your stub recorder is swallowing the invocation before the expect recorder has a chance to match it.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top