iOS UI Test, A journey through sweat… and more sweat (Part 1 : Stubbing Network)

I’m not currently drenched in sweat if you were to ask that. Oh, and this is a journal, not a tutorial.

Giorgy Gunawan
Swift2Go
4 min readAug 8, 2018

--

A random photo of a plant, find whilst searching the word ‘development’ in unsplashed. Photo by Markus Spiske on Unsplash

As I was trying to reach a mountain of gold called CI/CD for my iOS project, UI Testing is a valley that I have to conquer. It was a vast, plain, yet annoying experience in that place.

Xcode has given me a weapon to help me on my journey, that weapon is called XCUITests. This weapon has a great feat for accessing, interacting, and also asserting in the tests that I have to beat. But for stubbing network, it was just like a knife in a gun fight — it left me dead.

Overreacting? I don’t think so, in this valley, you need to isolate yourself so that everything goes smoothly. Getting a real response from a real server will only delay your test, if not fails it.

Staring at a blank space, I kept wondering what should I do to carry on… until it hit me.

OHHTTPSTUBS

I was glad that I found another weapon, called OHHTTPSTUBS.

OHHTTPStubs is a library designed to stub your network requests very easily.

— Taken from GitHub

I cried joyfully as I leapt through that vast plain.

Optimistically, I leapt faster and ran quicker to my mountain of gold. It was like I found a weapon to beat a dragon. My heart beat fast, and then it took a sudden stop (I’m not dead, chill). A barrier spawned in front of my face and its magic sent me flying back to the entrance of the valley.

What’s wrong? I look for clue, and there was one, written on the back of the weapon.

Dude, Hosted test are tricky

I assume it was meant for another valley called ‘Unit Testing’ (which is another journey I must take, but let’s get into that later).

That twinkling star of a hope suddenly strikes me down in despair.

But still, in despair i search for a more relevant weapon. Maybe it’s not a weapon I need, maybe a vehicle, or a house?

Conveniently, an old hermit was passing by and dropped something, a letter. And I read it aloud in my heart.

Dear son,

what you need is not a weapon, a vehicle, or a house. It’s a mock server.

try something, like swifter, or embassy

— your convenient stranger

Oh, okay…it seems that hermit was not fond of analogy. Striving for any clue, I took whatever path available to defeat this annoying valley. Strange as it may seem, I’ll try one of them.

Swifter

This ‘tiny HTTP server’ turned out to be my solution, my light to my darkness, my Romeo to my Juliet, my Tony Stark to my Doctor Strange, you get the point.

Following the quest guideline from this page, I immediately put in my first step to glory. But first I have to set my heart, my mind, and my build configuration and scheme.

With a new scheme I can have an environment for my app which connects to localhost:8080 (Where Swifter will host the HTTP server) as a base url from that server, I can get an expected response which I have to provide for each endpoint that my app originally in contact with. The expected responses can be provided with JSON files. Lock and loaded, I’m ready to go.

The first step, if I may say, is ‘lengthy but necessary’. I walked confidently while I ran through my diary of endpoint responses *cough* apiary *cough*. I put all of them in separate notes marked by JSON extensions, and put those notes in my resources.

After that, the rest was a stroll in the park. I set up the code to initialise the server everytime the test is being set up, and now my valley has its own mock server. And thus ended my journey…to get through the entrance of UI Test valley.

There are still a long way to go, it’s a mountain of gold anyway.

HTTP Stubber Class
Setting up with the test

Hi there curious reader, welcome to my first ever story in Medium. As I mentioned, this is not a tutorial, just a journal of what I have experienced — but if you were somehow share the same pain with me, maybe you could follow what I written above, or maybe you have a more efficient, I am open to inputs and suggestion.

The language may seem odd as i was trying to manipulate a coding story into an entertaining one. Maybe somewhere in the future I’ll write a part 2, who knows… this is fun for me anyway.

Oh and fyi, I’m not promoting or disapprove anything I wrote above.

If time is a luxury for you, please visit my website https://giorgyg.com/

--

--