Michael Long
Jan 9, 2023

--

Nice, but you can get it quite a bit smaller. Checkout the following gist.

https://gist.github.com/hmlongco/d1c43270084d8043555514aa375cc6d3

Just add the lazy debug var to any class you want to test and then reference it as shown in the test case. Since it's lazy it's not even constructed unless you're testing and since it's within a debug it doesn't exist in production code.

Fun fact. I used to add trackers like this to NSObject-based UIViewControllers using an associated object. No extra parameters needed.

One can also expend on the idea by not making the tracker lazy and then setting up some associated globals that they record into so we can run go through some process in an app and the back out again. If our count is X going in and coming out then we're clean. If more than that coming out then something's up.

Nice clean writing on the article.

--

--

Michael Long

I write about Apple, Swift, and SwiftUI in particular, and technology in general. I'm also a Lead iOS Engineer at InRhythm, a modern digital consulting firm.