This library is to enhance the use of the OCMock library, the Objective-C implementation of mock objects.
One problem with OCMock is that is impossible to mock singletons such as NSNotificationCenter and NSDate, without going through the extra steps described in the article Mocking Singletons with OCMock. This library takes the fundamentals of this blog post, and adds some custom categories for mocking various native objective-C singletons, such as NSNotificationCenter.
To start using the library you can build it as a static library in the usual way, but a quick and easy method is as follows:
I have published instructions on one way to add the OCMock Library to your project in Xcode 4.
Copy the files inside OCMockSingletonExtension into your project, making sure you add them to the Unit Testing target only.
- Import the header NSObject+SupersequentImplementation.h every time
- Import the header needed for the singleton you want to mock
- Heres an example of how to use NSNotificationCenter+UnitTests
#import "NSObject+SupersequentImplementation.h"
#import "NSNotificationCenter+UnitTests.h"
- (void)testNSNotificationCenter
{
//Call the createMockNotificationCenter or createNiceMockNotificationCenter to return a mock object whenever you call defaultCenter
id mockNotificationCenter = [NSNotificationCenter createMockNotificationCenter];
//Now [NSNotificationCenter defaultCenter] will return the mockNotificationCenter
STAssertEquals(mockNotificationCenter, [NSNotificationCenter defaultCenter], nil);
//Now call releaseInstance so [NSNotificationCenter defaultCenter] will return the original defaultCenter again
[NSNotificationCenter releaseInstance];
}
Fork the project and add your own singletons to help grow this.