Cocoa TDD 和单例
我正在使用 Cocoa 进行 TDD,我想问 - 测试单例类的正确方法是什么?我对初始化和检索部分很好奇。
我正在考虑做类似的事情:
MySingleton *singleton1 = [MySingleton sharedInstance];
MySingleton *singleton2 = [[MySingleton alloc] init];
STAssertEqualObjects(singleton1, singleton2, @"Objects were not equal: %@ and %@", singleton1, singleton2);
我还应该测试什么?我是否应该尝试在可能的竞争条件下测试行为(测试 @synchronize
语句)?
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 should test for? Should I even try to test the behavior under possible race conditions (test the @synchronize
statement)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您的单例没有可写状态,那么您根本不必担心这一点。
如果您的单例具有可写状态,那么它可能根本不应该是单例。
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.