I'm doing TDD with Cocoa and I wanted to ask - what is the correct way of testing a singleton class? I'm curious about the initialization and retrieval part.
I'm thinking of doing something similar to this:
MySingleton *singleton1 = [MySingleton sharedInstance];
MySingleton *singleton2 = [[MySingleton alloc] init];
STAssertEqualObjects(singleton1, singleton2, @"Objects were not equal: %@ and %@", singleton1, singleton2);
Anything else I sh开发者_StackOverflow中文版ould test for? Should I even try to test the behavior under possible race conditions (test the @synchronize
statement)?
If your singleton has no writable state, then you don't have to worry about this at all.
If your singleton has writable state, then it probably shouldn't be a singleton at all.
精彩评论