Okay, that title may be misleading, but I can’t resist a cheesy blog post title. This post is about my foray in testing. Code can often be made better, and as developers we are constantly learning and growing on this path to better code.
I’ve just completed a project that has added a few things on my list to learn and improve on, but the main one that has been drawing my attention is testing.
Surprisingly, I’ve never worked with tests in my previous roles, and as I consume more material on the topic I realize more and more the importance of including tests in projects. So I decided to create a test project after watching a couple of WWDC videos (these two are pretty good: one and two - this test project is inspired by the first video) and checking out a few articles .
Particularly I wanted to test out what I thought might be hardest to test with mocking, networking, since everything else of what I’ve seen in testing seems pretty straight forward so far. So I grabbed some ‘fake JSON’ (if you ever need fake JSON for a tutorial, check out this link), and started the project that is shown below.
In this example I’m taking a hint from the Apple video linked above and creating mock objects, which do exactly what the names says. It mocks certain parts of your code, in this case it will be data and objects.
My Networking Class depends on URLSession to download data from the endpoint provided. So this is what will be mocked, then reimplemented when testing. So yeah, I somehow found myself also learning dependency injection while trying to get experience with tests.
The screenshots of code have comments along the way with explanations, of general mocking and testing information.
Now the most important part, the actual test!
So now I think I have a very basic understanding of mocking for tests, the beginnings of testing a network request, and dependency injection, so I’ll only continue to build on what I’ve learned in last in the past day. I may have jumped the gun on this, practicing tests with the use of mocks, I just really found the concept interesting and wanted to give it a try. Now, I must backtrack and start at the beginning. I have my eyes on the Hacking With Swift book for testing, since I’m finding articles aren’t 100% reliable.
This process also got me thinking about:
Testing asynchronous code, especially for networking tests
Building my test to fail. I built these tests thinking the entire way on how to get the test to pass, but I think I need to look at both sides.
Structuring my code based on my tests… outside of this individual project I went back to previous code that I’ve done to input some test and had to start restructuring my code.
Understanding where mock files fit in my code. The best way to structure them and the best place to places them and their protocols.
I’m happy I got the chance to work on the recent challenge mentioned at the start of the post, it has introduced me to a whole other section of iOS that I’m excited to learn more about!