Eimantas
Eimantas

Reputation: 49354

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 should test for? Should I even try to test the behavior under possible race conditions (test the @synchronize statement)?

Upvotes: 1

Views: 99

Answers (1)

J. B. Rainsberger
J. B. Rainsberger

Reputation: 1203

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.

Upvotes: 1

Related Questions