mercredi 23 mars 2016

Verify a method was called from another object OCMock

I have two classes.

Object 1:

- (void) methodA {
    ObjectB objectB = [[ObjectB alloc] init];
    [objectB methodB];
}

And Object 2:

- (void) methodB {
     // Does something
}

Using OCMock, how do I verify that methodA calls methodB? I'm setting the test up like the following:

id mock = OCMClassMock([Object2 class]);
OCMStub([mock methodB).andReturn(nil);

[self.object1 methodA];
OCMVerify([mock methodB]);

The test class is testing object1, but I'd like to verify that it calls the method on object2. When running this test, I get the failing message:

Method methodB was not invoked.

I'm still really new to mocks/stubs. It's entirely possible that I'm structuring the test wrong. The mock confuses me a little because I'm testing Object1, but trying to verify something on Object2.

Am I thinking correctly when I am setting up this test? If not, how should I approach this?

Aucun commentaire:

Enregistrer un commentaire