开发者

Cocoa TDD and singletons

开发者 https://www.devze.com 2023-04-08 17:29 出处:网络
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 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

精彩评论

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