Does OCMock automatically use a laughable instance in test code?

I am new to OCMock .

I use dispatch_once()created a singleton class MyManager:

@implementation MyManager

+ (id)sharedInstance {
    static MyManager *sharedMyManager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedMyManager = [[self alloc] init];
    });
    return sharedMyManager;
}

I have a method in a class Schoolthat uses the singleton above:

@implementation School
...
- (void) createLecture {
  MyManager *mgr = [MyManager sharedInstance];
  [mgr checkLectures];
  ...
}
@end

Now, I want to unit test this method, I use the partial MyManager layout:

- (void) testCreateLecture {
  // create a partially mocked instance of MyManager
  id partialMockMgr = [OCMockObject partialMockForObject:[MyManager sharedInstance]];

  // run method to test
  [schoolToTest createLecture];
  ...
}

I noticed that with OCMock, after creating a partial layout of my singleton instance, MyManagerwhen I run my test method, it automatically uses the partially fabricated instance.

This is a little strange for me, since in my test example above I only created a partial layout of the instance MyManagerwithout entering it into the class MyManager,

OCMock , [MyManager sharedInstance] ? - ?

+4
2

partialMockForObject , .

() . , sharedInstance , . .

, , , .

+3

Mocks . OCMock , , , , mock. , (mocked implementation) ( ). .

+2

All Articles