开发者

Cocoa TDD and singletons

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜