How to Make Swift Mock Objects More Useful

May 2, 2017 — Leave a comment

I’ve written about my experience of going to try! Swift Tokyo 2017. Now thanks to the video and transcript provided by Realm, I can also share the talk I gave: “Making Mock Objects More Useful”.

Speaking at try! Swift Tokyo on Swift Mock Objects

I start by showing the basics of how to make a mock object by hand. But this easily leads to fragile tests because the assertions are overspecified. We need ways to make tests more malleable, with mocks that are more flexible.

To learn more, see the Realm video, or read the transcript.

You may want to increase the playback speed of the video. Because there was simultaneous interpreting, I spoke very slowly, with unusually crisp enunciation. So I was happy when an attendee tweeted in Japanese, “Thanks to the slow speaking style, I could listen without the interpreter for the first time!”

Equality assertions can be too strong

One of my points in the talk is to avoid overusing equality. This is something I’ve addressed before, outside of mock objects. See Swift Equatables: Do We Really Need Them for Testing?

Sample code as mock objects reference

When I kept referring to my own sample code for my day job, that gave me a good feeling. If this talk was useful for my own work, I knew it would help others, too. I still check it to remind myself how to write mock objects in Swift!

Reducing boilerplate going forward

Writing mock objects by hand isn’t hard, but it is tedious. There’s a new project that looks very promising, called Mimus. It should take care of most of the boilerplate code, making it easier to write and maintain Swift mocks.

Do you have any questions about mock objects in Swift? Please share in the comments below.

Jon Reid

Posts Twitter Facebook Google+

I've been practicing Test Driven Development (TDD) since 2001. Learn more on my About page.

No Comments

Be the first to start the conversation.

Leave a Reply

Text formatting is available via select HTML.

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> 

*