[swift-evolution] Mocking in Swift

Mark DiFranco mpdifran at gmail.com
Thu Sep 8 23:46:31 CDT 2016


This is my first post, let me know if I’m doing anything wrong!

Has there been any discussion on implementing mocking in Swift for tests?

I’ve been taking a TDD approach in my Swift apps, and I have resorted to hiding all my classes behind protocols (which encapsulates the public methods and properties for each class) in order to easily mock out dependencies in tests via manually created mock classes. 

This has several downsides:

1) Protocols cannot conform to Equatable, reducing the number of useful collection methods available that require that protocol.
2) This process becomes quite cumbersome when classes require via method parameter a class that also needs to be mocked (and thus is hiding behind a protocol). The CoreBluetooth classes are an example of this.
3) There is a layer of code (directly above external dependencies) that needs to be hidden behind a protocol, thus leaving a layer that is un-testable, and usually very boilerplate.

I can provide examples of the current method I’m using to better highlight its shortcomings, if needed.

Cheers, 
Mark



More information about the swift-evolution mailing list