1

Is this bad practice?

@property (nonatomic, weak) NSObject<TTModalDelegate, TTTimeEntryVCDelegate> *delegate;

I'm trying to test something using the Kiwi framework by mocking this delegate that conforms to two protocols. However, it appears this is not possible from reading the docs and internet searching. I can create a mocked delegate that conforms to a single protocol like so:

NSObject *mockDelegate = [KWMock mockForProtocol:@protocol(TTTimeEntryVCDelegate)];

I'm wondering if Kiwi doesn't have the ability for a mock to conform to multiple protocols because it is bad practice. If so, why is it bad practice?

abc123
  • 8,043
  • 7
  • 49
  • 80

1 Answers1

4

Creating classes that conform to multiple protocols isn't in itself bad practice; that's why the syntax is there. However, as Apple points out in their documentation:

If you find yourself adopting a large number of protocols in a class, it may be a sign that you need to refactor an overly-complex class by splitting the necessary behavior across multiple smaller classes, each with clearly-defined responsibilities.

Nor is there theoretically anything wrong with requiring a delegate conform to multiple protocols.

However, you should consider making your API less restrictive by creating distinct properties for each protocol. Think dataSource and delegate properties of UITableView. Not only will this make your API more flexible, you can then test your class using Kiwi (which simply has a limitation of not being able to mock multiple protocols).

Just like UITableView's relationship with UITableViewController, there's nothing to stop you pointing both properties at the same object.

followben
  • 9,067
  • 4
  • 40
  • 42
  • Well said. To add to what followben said, keep the Single Responsibility Principle (http://en.wikipedia.org/wiki/Single_responsibility_principle) in mind: if an object conforms to multiple protocols, it most likely fails to adhere to the SRP. – Gabe Apr 23 '14 at 20:36