Proper usage of OCMock with Singleton

177 Views Asked by At

I am wondering what the proper way to use singleton. I have read the following Mocking Singletons The post is old and for right now we are using OCMock with the help of OCMPartialMock for instance:

@interface ManagerVisitorTests : XCTestCase
@property (nonatomic) id partialManager;
@property (nonatomic) id partialHelper;

@end

@implementation ManagerVisitorTests

static NSString *const key = @"key"; 
- (void)initSDKWithoutVisitor {
    self.partialManager = OCMPartialMock([manager sharedManager]);
    NSDictionary *decodedAppkey = @{};
    OCMStub([self.partialManager methods:OCMOCK_ANY]).andReturn(decodedAppkey);
}

- (void)testInitNoVisitor {
    [self initSDKWithoutVisitor];
    [self.partialManager initSDKWithoutVisitor:key];
    XCTAssertFalse([manager sharedInstance].isInitCalled, @"called");
    //OR
    XCTAssertFalse([self.partialManager sharedInstance].isInitCalled, @"called");
    [self stopAllMocks];
}

- (void)stopAllMocks {
    [self.partialManager stopMocking];
    [self.partialHelper stopMocking];
    self.partialManager = nil;
    self.partialHelper  = nil;
}


@end

I must admit that I don't fully understand how the OCMock works under the hood but it seems that OCMPartialMock should do the work according to the docs This means that calls using a reference to the real object, even including self in methods where the object calls itself, are also affected by stubs and expectations

Please advice if its a good approach for the singleton case?

0

There are 0 best solutions below