---
title: "Adventures in iOS Dev Automation"
source: "/writing/adventures-in-ios-dev-automation/"
---

Article

# Adventures in iOS Dev Automation

Sean Berry

December 12, 2018

At Livefront, [we](/writing/unit-testing-race-conditions-by-creating-chaos-swift) [love](/writing/4-hard-won-lessons-from-working-on-a-professional-software-engineering-team) [unit testing](/writing/practical-dynamic-type-part-2-testing) . Part of that is setting up _mocks_ -- fake objects you use with dependency injection to make sure the right methods are being called.

Android has the [Mockito](https://site.mockito.org/) framework where you can trivially make mocks and dictate return values. We don't have anything that slick in Swift due to its reflection restrictions. There's [Cuckoo](https://github.com/Brightify/Cuckoo) and SwiftyMocky, but I found them a bit onerous. They both generate helpful code (_and I'm all for_ [_generating code_](/writing/introduction-to-swiftpoet) ), but way too much. **I only want to generate code I'm going to use.**

I'm going to show you how to automatically generate a default implementation and mock of a Swift protocol. Computers make us more efficient. Let's use the computer to make our computer-use more efficient.

## Sourcery -- generating Swift

[Sourcery](https://github.com/krzysztofzablocki/Sourcery) is a command line tool that wraps around [Stencil](https://github.com/stencilproject/Stencil) (a Swift template language). It also parses your file for types and [adds helpful tags and filters](https://cdn.rawgit.com/krzysztofzablocki/Sourcery/master/docs/writing-templates.html) .

Here's what my template looks like to generate a default implementation and mock for the protocols Sourcery finds:

It's pretty simple. All it does is iterate through the methods and calls the `methodSig` macro to write them out properly. You know when Xcode notices you haven't implemented a protocol correctly and kindly offers to throw in some stubs for you? This is about the same. If you want a more advanced mock, check out Sourcery's [AutoMockable](https://github.com/krzysztofzablocki/Sourcery/blob/master/Templates/Templates/AutoMockable.stencil) .

## Apple's Automator

To run Sourcery, you need to execute the following command:

sourcery --sources ~/Path/To/Protocol.swift --templates ~/Path/To/Template.stencil --output ~/Output/Folder

I certainly don't want to be typing that out every time, so at a minimum we're throwing that in an alias. But we can do even better. Instead of having a terminal window opened up in our project's directory all the time, we can wrap a bash script using [Automator](https://support.apple.com/guide/automator/welcome/mac) and make it accessible from the dock.

Automator comes with your Mac and is pretty simple to use. Create a new automation and add an 'Run Shell Script' step. It should look something like:

Save it as an app and move it to your dock! When you drag your Swift file onto it, the script will receive the file path as the first argument.

Here's what the final workflow looks like:

🔥Success! 🔥

This is not a generalized solution. It's a response to a tedious task I noticed popping up regularly. I like to think of it as refactoring my work day. Am I repeating myself? Can I extract these tasks and automate them?

These computer machines are powerful. Automation can save you time and reduce the opportunity for human error.

Thanks to [Keehun Nam](https://medium.com/u/7f64a56815cd?source=post_page-----b18c3abd835a--------------------------------) for the inspiration!

_Sean is trying to get the computer to do what he wants at_ [_Livefront_](http://www.livefront.com) _._

-   Sean Berry

    Software Engineer

    Sean Berry

### Tags

[Digital Engineering](/insights/all/?topic=digital-engineering) [Mobile Apps](/insights/all/?topic=mobile-apps)

### Share

-   Copied

## View these next.

[See all](/insights/all/?type=writing)

-   [

    Article

    How To Sabotage Your Project Using Inconsistency

    Collin Flynn

    ](/writing/how-to-sabotage-your-project-using-inconsistency/)
-   [

    Article

    Unit Testing race conditions by creating chaos (Swift)

    Sean Berry

    ](/writing/unit-testing-race-conditions-by-creating-chaos-swift/)
-   [

    Article

    UIApplicationDelegate call sequence reference

    Sean Berry

    ](/writing/uiapplicationdelegate-call-sequence-reference/)
