# Staying Sane with Cuckoo and Code Generation

DevFeed: [Staying Sane with Cuckoo and Code Generation](<https://devfeed.tech/articles/staying-sane-with-cuckoo-and-code-generation-22314.md>)

Original publisher: [Read original article](<https://www.thecodedself.com/cuckoo-and-code-generation/>)

Author: Keegan Rush

Published: 2017-10-30T00:00:00Z

Content type: tutorial

Language: en

Sources: [The Coded Self](<https://devfeed.tech/sources/the-coded-self.md>)

Topics: [Swift](<https://devfeed.tech/topics/swift.md>), [Mocking](<https://devfeed.tech/topics/mocking.md>), [Code generation](<https://devfeed.tech/topics/code-generation.md>), [Boilerplate](<https://devfeed.tech/topics/boilerplate.md>), [Test-driven development](<https://devfeed.tech/topics/tdd.md>), [Objective-C](<https://devfeed.tech/topics/objective-c.md>)

Tags: [c](<https://devfeed.tech/tags/c.md>), [code](<https://devfeed.tech/tags/code.md>), [code-generation](<https://devfeed.tech/tags/code-generation.md>), [consistency](<https://devfeed.tech/tags/consistency.md>), [library](<https://devfeed.tech/tags/library.md>), [mocking](<https://devfeed.tech/tags/mocking.md>), [swift](<https://devfeed.tech/tags/swift.md>), [tdd](<https://devfeed.tech/tags/tdd.md>), [tests](<https://devfeed.tech/tags/tests.md>), [unit-tests](<https://devfeed.tech/tags/unit-tests.md>)

## AI overview

The article introduces Cuckoo, a Swift mocking framework that uses code generation to create mock boilerplate. It contrasts this approach with reflection-based mocking in Objective-C and discusses how code generation fits Swift's type-safety goals.

## Source excerpt

Please note that this post won't go over the basics of Cuckoo, as the README provides a great introduction to the library. Do you write unit tests? You should. Writing mocks for my tests seemed to be a lot easier in Objective-C. OCMock makes great use of Objective-C's dynamic nature and it makes mocking a breeze. However, the OCMock website has this to say: Disappointing. Accurate. Alright, off to find an alternative. The best that I could find was SwiftMock, but that came with an even sharper let-down: It seems that Swift's lack of reflection means that we'll be doomed to doing things manually and rewriting the same type of code, again and again. So, I resorted to rolling my own mocks. It's not that bad. Really. I built a little mocking framework of my own based off of a Mock protocol and use of Swift's basic reflection, and all was happy in the world of TDD. When you're writing a lot of code and all of the tests that should come with it, all of the time spent writing boilerplate for your mocks gets tiring. I was hungry for something more efficient. Then, I was introduced to Cuckoo. Cuckoo is a mocking framework for Swift that uses code generation to create the boilerplate that you'd otherwise be writing yourself. It comes with a lot of functionality that I think would be far too tedious and repetitive to do on your own. It also ensures consistency, which is something that I've found to be sorely lacking in the world of hand-rolled mocks. Code Generation versus Reflection Most programming languages use reflection to dynamically perform things that we would otherwise have to write repetitive boilerplate for. OCMock uses reflection to create mock objects. It doesn't actually create a new class for each type you wish to mock. Reflection allows languages like Objective-C to dynamically create mocks, check for equatability, and perform JSON serialization. As of yet, this isn't an easy feat in Swift. The Swift language shuns dynamic behaviour and encourages safer methods