SoatDev IT Consulting
SoatDev IT Consulting
  • About us
  • Expertise
  • Services
  • How it works
  • Contact Us
  • News
  • June 8, 2023
  • Rss Fetcher

Sourcery — Generate Mocks for iOS Projects

Stop writing boilerplate and code that needs frequent changes!

https://unsplash.com/photos/6sAl6aQ4OWI

Sourcery is a powerful code generator for Swift projects that I implemented to the project at my current job to stop writing mocks and helpers for our tests.

When I watched a presentation about Sourcery, my mind was blown thinking about how my daily code routine could be automated. That week, I spent extra hours bringing that idea to real life, and an example of the result is here.

I can’t share the code I created to use at work, but here’s an example of how powerful it is!

This article is divided into two parts. The first is an example of how it works, and the second is a deep look at the code needed to generate files.

Table of Contents

  1. Adding Sourcery to a project
  2. Create models
  3. Create Sourcery configuration file
  4. Add templates
  5. Create build phase script
  6. Let’s run
  7. Option to use custom default parameter at mock

1. Adding Sourcery to project

  • Open Podfile
  • Add the following code
  • Run pod install
pod 'Sourcery', :subspecs => ['CLI-Only']

For this example, I added Sourcery using cocoapods, but you can choose any method listed on the Sourcery Installation page.

2. Create models

I create two models for this, but in a real case, you will use the models of your project. Here’s the code:

public struct Client: Codable {
public let id: Int
public let name: String
public let age: Int
public let isFistAccess: Bool
public let address: Address
}

public struct Address: Codable {
public let street: String
public let city: String
public let state: String
public let homeNumber: Int
public let postalCode: String
}

3. Create Sourcery configuration file

The Sourcery.yml is your main configuration file:

sources: the paths to include and exclude files
templates: the paths to template files
output: the path where Sourcery will write the files
args: the local variables to send to Sourcery template files

sources:
include:
- ../../SourceryExample/Models
exclude:
- ../../SourceryExampleTests

templates:
- ../Templates/ModelsMock/ModelsMock.swifttemplate

output:
../../SourceryExampleTests

args:
projectName: ${PROJECT_NAME}
  • Create Sourcery.yml in the following path Meta/Configuration
  • Add it to your project, then uncheck all the targets
importing configuration file

4. Add templates

Add the Templates inside the Meta folder.

The details of Templates (ModelsMock and Core) will be described in part 2 of this article.

Importing template files

5. Create the build phase script

Create a new Run Script with the following code to execute Sourcery when not building for release.

#!/bin/bash
if [ "${CONFIGURATION}" != "Release" ]; then
$PODS_ROOT/Sourcery/bin/sourcery --config "${PROJECT_DIR}/Meta/Configuration/Sourcery.yml" --prune
fi

Reorder the script to execute before Compile Sources .

It’s necessary to prevent breaking the next build if the generated file breaks. With this change, the Xcode will always call “Run sourcery” to generate files before the compile project.

Build phases with “Run Sourcery”

6. Let’s run the project

Now, it’s magic time. You need to build the project to see the newly generated file.

First, you need to import the generated file into your project:

Importing the generated file

But, for the next builds, the file will be updated automatically.

Sourcery updating generated file

7. Option to use custom default parameter at mock

When I started adding Sourcery to my current job’s project, my biggest fear was delivering something new to my team that I was still learning about, but I wanted to reduce their workload without breaking anything, even if this new solution wasn’t 100% ready.

To prevent breaking anything, I used Sourcery Annotations to create a bypass that enabled the engineer to write the default value for the generated mock.

With this, even if the template failed to recognize the right value, the engineer could bypass and tell Sourcery the right value for that case.

This article’s code is on the main branch of my GitHub page:
https://github.com/jnevesjunior/sourceryExample


Sourcery — Generate Mocks for iOS Projects was originally published in Better Programming on Medium, where people are continuing the conversation by highlighting and responding to this story.

Previous Post
Next Post

Recent Posts

  • Elon Musk is lobbying lawmakers on driverless vehicle rules
  • Yep, X was down again
  • TechCrunch Mobility: A ride-sharing pioneer comes for Uber, Tesla loses more ground, and dog-like delivery robots land in Texas
  • Elon Musk departs DC with a black eye and a tattered reputation
  • Introducing: AI-Assisted Coding Assessments and Interviews

Categories

  • Industry News
  • Programming
  • RSS Fetched Articles
  • Uncategorized

Archives

  • May 2025
  • April 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • July 2024
  • June 2024
  • May 2024
  • April 2024
  • March 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • September 2023
  • August 2023
  • July 2023
  • June 2023
  • May 2023
  • April 2023

Tap into the power of Microservices, MVC Architecture, Cloud, Containers, UML, and Scrum methodologies to bolster your project planning, execution, and application development processes.

Solutions

  • IT Consultation
  • Agile Transformation
  • Software Development
  • DevOps & CI/CD

Regions Covered

  • Montreal
  • New York
  • Paris
  • Mauritius
  • Abidjan
  • Dakar

Subscribe to Newsletter

Join our monthly newsletter subscribers to get the latest news and insights.

© Copyright 2023. All Rights Reserved by Soatdev IT Consulting Inc.