📈 Unit Testing in Swift: Behavioural Assertion

Kewin
The Startup
Published in
6 min readJul 9, 2020

--

You’ve reached the fifth and final part of the Unit Testing in Swift series!
In this article we will talk about behavioural asserting and look into further enhancing the mock classes, that we created in the previous article, to make behavioural assertion easy in your own test suite. If you are new to unit testing and want to learn how to start unit testing with Swift in Xcode, I suggest you take a step back to Part 1: The Fundamentals of the series, before reading further. As basic mocking is required for the kind of behavioural assertion we will be implementing in this article, I recommend reading and understanding Part 4: Mocking before continuing with this article. The examples in this article, use the classes A, B and C as defined in Part 3: Proper Architecture.

Behave, please!

Being computer scientists our mindsets are naturally based on logic and it’s no wonder that the first thing that comes to our mind when unit testing, is to test for the logical outcome of the given function that we are testing. Take our unit test of class A‘s checkAnswer() (from Part 4: Mocking) function for example:

--

--

Kewin
The Startup

Lead iOS Engineer @ Jabra 🇩🇰 Full time 💻📲 — part time ‍🏊🏼🚴🏼🏃🏻⛷⚽️