Home > Software design >  Did not record an invocation in OCMStub/OCMExpect/OCMReject
Did not record an invocation in OCMStub/OCMExpect/OCMReject

Time:09-16

I'm trying to mock a Singleton class in one of my unit tests. The way I'm creating my mock is like this:

MySingletonController *mockController = OCMClassMock([MySingletonController class]);
OCMStub([MySingletonController sharedController]).andReturn(mockController);

The complete error that I got is:

Did not record an invocation in OCMStub/OCMExpect/OCMReject.
Possible causes are:
- The receiver is not a mock object.
- The selector conflicts with a selector implemented by OCMStubRecorder/OCMExpectationRecorder. (NSInternalInconsistencyException).

One important thing to mention is that the shared instance is not something that instantiates the class, it just returns a variable. That variable is assigned during the init (which occurs) during the nib load. But I really doubt that this has something to do with the problem.

I'm not completely aware of what could cause the OCM error that I'm seeing.

CodePudding user response:

The second line should be as follows:

OCMStub([mockController sharedController]).andReturn(mockController);

CodePudding user response:

The problem is how I was declaring the OCMStub. My sharedController method is a class method. Then it needs to be declared differently for OCMStub.

MySingletonController *mockController = OCMClassMock([MySingletonController class]);
OCMStub(ClassMethod([(id)mockController sharedController])).andReturn(mockController);

After changing my code as above, the test started to work properly.

  • Related