I am writing a unit test for a class Foo, which has a collaborator, Bar. I want to use a manually-built stub implementation of Bar in Foo's test.
If I were doing this in Java, I would give Foo a BarFactory collaborator and inject a MockBarFactory in Foo's test that always returned my StubBar.
I know this technique will work fine in Objective-C but it doesn't strike me as a particularly idiomatic thing to do in a dynamic language. I am wondering if I can do anything tricky that will cause [[Bar alloc] init] to return StubBar while I'm running my unit test but give me the normal implementation of Bar in "real life".
Or is the obvious factory pattern the most appropriate thing to use in this case?