Cuckoo

screenshot of Cuckoo

Boilerplate-free mocking framework for Swift!

Overview

Cuckoo is a powerful mocking framework for Swift that was created to fill the gap of a proper Swift mocking framework. It provides a DSL (Domain-Specific Language) that is similar to Mockito, making it easy for developers coming from Java/Android to use. The framework has two parts: the runtime and the OS X command-line tool called CuckooGenerator. Cuckoo uses a compile-time generator to generate supporting structs and classes that are used by the runtime in the test target. This allows for mocking of overridable things based on inheritance and protocol adoption.

Features

  • Inheritance: Supports mocking of grandparent methods through inheritance.
  • Generics: Provides support for mocking methods with generic types.
  • Simple Type Inference: Enables type inference for instance variables, which can be overridden by explicitly specifying the type.
  • Objective-C Mocks: Supports mocking of Objective-C classes using OCMock.

CocoaPods

To install Cuckoo using CocoaPods, add the following line to your test target in your Podfile:

pod 'Cuckoo'

Then, add the following Run script build phase to your test target's Build Phases above the Compile Sources phase:

${PODS_ROOT}/Cuckoo/run generate

Make sure the run script is above the Compile Sources phase to avoid any race condition errors during build. Additionally, you may need to add the path of the OUTPUT_FILE into the "Output Files" section of the build phase to avoid any regeneration issues with new changes.

Swift Package Manager

To install Cuckoo using Swift Package Manager, do the following steps in Xcode:

  1. Navigate to File > Swift Packages > Add Package Dependency.
  2. Enter https://github.com/MakeAWishFoundation/Cuckoo.git as the package repository URL.
  3. Follow the prompts to complete the installation.

Summary

Cuckoo is a powerful mocking framework for Swift that fills the gap of a proper Swift mocking framework. It provides a DSL inspired by Mockito and supports inheritance, generics, simple type inference, and Objective-C mocks. The framework is easy to install using CocoaPods or Swift Package Manager, making it accessible to a wide range of Swift developers.