Mocking data for UI testing in Xcode 7

Over the passage of time working on different projects i’ve seen a lot of different automation frameworks to handle UI testing. Mostly quite similar, they rely on the concept of having deterministic mock data that will power your views, they then drive your views through a user journey to assert an expected outcome. Most of these frameworks follow the Behaviour Driven Development (BDD) approach to writing tests through their adoption of Cucumber, I won’t go into the details of these here as its not really relevant.

Xcode UI testing however takes a different stance and on my current project, we’ve used it more as a “no, don’t unit test that, its a view controller, write a UI test” style tool, rather than a fully fledged automation framework. What this really means is, we’re asserting things like “the menu only contains 5 items that match our mocked data” or “when you hit login an activity indicator view is displayed”; whereas in UI Automation land we’d be testing things like “given i’m a logged out user, then I enter text into username and text into password, then I hit login, I should be presented the welcome screen”… Or similar. So effectively we’re testing small discreet units of UI within our application.

The two things we soon discovered we’d need for this are:

 The ability to inject dependencies where possible so we can load views without always necessarily progressing through a user journey to them.

The ability to test our views deterministically. This means we require MOCK data.

Why don’t we just go to the network, we can guarantee our API? Well, basically because you can’t. If you can run your test suite in a nuclear bunker, surrounded by a faraday cage with no outward or inward connectivity, then you’re onto a winner. The ability to be deterministic, to be certain that if your test fails its not because of some outward problem, but indeed due to a problem with your implementation, is vital for this specific type of testing. Other testing strategies such as end-to-end etc can accomodate this.

I’ve seen a few approaches to mocking data over the past decade. The common theme seems to be either one of:

Running a proxy that will returned canned responses to requests, configuring your network to use it as appropriate..

Littering all of your network code with #ifdef UI_AUTOMATION… and returning canned responses from an NSBundle.

In each individual test case, creating stubs and mocked responses for HTTP calls on the fly

All work, but it can be a little easier depending on your use case. SuperMock is inspired by both… a framework that you can drop into an existing code base, provide it with a simple .plist that maps network calls to canned responses in your NSBundle, functions as an NSURLConnection/NSURLSession proxy, but runs as part of your application test target, runs no server, requires no network and only leverages built in components of Foundation.

Example Scenario:

You have a menu in your application, which is dynamically driven from a web service, the contents of this menu can change at any time, but you’d like to assert that given some data, it populates and draws correctly on the screen.

Given the aforementioned problems… bring on the MOCK!

Example of the required Mocks.plist

Example of the required Mocks.json

By defining your original/real URL in

Mocks.plist

of SuperMock and running 1 line (ok maybe 2) of code in your AppDelegate, you are able to guarantee the value of your menu items in your application. Making your UI Test case very simple to write and very reliable to run.

How to implement SuperMock in your App.

How to implement SuperMock in your App.

Now when your HTTP client fires off a request for

http://mike.kz/api/menu.json

it will instead get returned

file://....menu_MOCK.json

from the NSBundle you provided earlier. In your UI Tests (whatever framework you use), you can now assert based upon these MOCK values. Whats advantageous is that there is no more work to be done and no extra code in your test cases to stub out responses.
This is a short whirlwind tour of my latest little pet project, but expect to see feature like dynamic override of mocks (for use when you want the same URL to return different responses upon each request) and some more real world scenarios.

I’ll be using this framework a lot myself in my personal projects over the coming months, so I may tweak / change / improve / try not to break it, in the process. Right now its functional but not complete and needs a new README.md ?

 

https://github.com/michaelarmstrong/SuperMock / @ArmstrongAtWork

 

Mockacino a lightweight ruby/sinatra API Mocking script

Hi Guys, so i’ve been working on a new app recently for a client of mine, currently there is no API… so in the meantime I decided to knock up a quick few lines of ruby to get a mocking api up and running… to explain this better, I need not do more than paste my README.md here.. Enjoy 🙂

Mockacino

A very simple and easy to use MOCK API server that serves static JSON written in ruby/sinatra.


NOTE: This is a super simple, super fragile MOCKING SERVER Intended so you can test routes and mock an API with static jso whilst you’re still building the real production API. DO NOT EVER use this in production… Seriously. It breaks a lot and will if you try… DONT. Absolutely 0 effort went into it, therefor 0 Warranty. Use if you dare.

DOCS:

Folder structure defines API calls…. return.json is what gets served.

site_root ->;
    [ http method ] ->; [ api call route ] ->; [ json response contents of call return.json ] 

e.g.

site_root ->;
    GET ->; users ->; return.json
    POST ->; users ->; create ->; return.json

If you have static assets you wanna reference in the json, plonk them in the ASSETS folder

e.g.

site_root ->; file.jpg

You can call http://yourhost:port/ASSETS/file.jpg

Here’s the directory structure of the sample project included here

mockacino.rb  
./site_root/ASSETS
./site_root/GET
./site_root/POST
./site_root/GET/sheep
./site_root/GET/sheep/create
./site_root/GET/sheep/return.json
./site_root/GET/sheep/create/return.json

Which supports calls like…

http://localhost:4567/sheep
http://localhost:4567/sheep/create

And gives responses from the static result.json file like…

{
    "sheep": [
        {
            "id": "1234",
            "name": "Dolly Two",
            "url": "http://mike.kz/sheep1234",
            "assets": {
                "small_image": "http://cloned.sheep.com/ASSETS/small.jpg",
                "large_image": "http://cloned.sheep.com/ASSETS/large.jpg"
            }
        }
    ]
}

Usage:

gem install sinatra
ruby mockacino.rb
 

Pasti.ng An Engineers Pastebin re-invented.

As part of my ongoing pledge to be less selfish, i’ve decided to spend a few hours making a nice little Paste Bin website.

It uses Couchbase (a NoSQL storage database server) and Ruby on Rails along with a touch of bootstrap. I’m gonna be adding thing such as “re paste” in-line editing and self-destructing pastes as I get time. But until then, its nice little pet project for me to ponder on.

Once you paste it maintains the formatting of your code and even supports Unicode.

It should be lightening fast (even though its ruby) and stable + it has a cool domain name… what else do you need. Paste your heart out.

http://pasti.ng

Enjoy!

 

Ubuntu 13.04 on a Late 2012 Mac Mini

EDIT: Tonight i’ll be picking up one of my Mac Mini’s from the Datacenter to get Ubuntu 13.04 up and running! expect a full guide with drivers here shortly 🙂

So Ubuntu 13.04 LTS recently was released, It comes with the new 3.8.0-19 upstream of the Linux Kernel so I thought I’d check it out!

Although our patched 12.04 and 12.10 Ubuntu’s use version 3.124c of the tg3 NeXtreme drivers from Broadcom which have Mac Mini support… The version in Ubuntu 13.04 (3.128c) seems to have had this removed!

A simple run of modinfo tg3 | grep 1686 reveals sadly that support for detection of the Mac Mini Ethernet hardware seems to have been removed during 3.124 and 3.128 of the Broadcom tg3 drivers.

I’m likely to install 13.04 on a Mac Mini sometime soon so will update this post with a proper howto and any good news I encounter but I don’t think its good news…

lsmod | grep Ethernet returns
01:00.0 Ethernet controller: Broadcom Corporation Device 1686 (rev 01)

whilst modinfo tg3 | grep 1686 on our modified 12.04/12.10 machines using the NeXtreme driver from this blog returns:

alias:          pci:v000014E4d00001686sv*sd*bc*sc*i*

however on 13.04 returns nothing.

 

Instantly gain insight on someone else’s iOS App Architecture with MADebugTools

Just a quick post to say i’ve posted my first piece of code in over 5 years to GitHub. Its a clever little Objective-C iOS Category on UIViewController that seemlessly overlays a UILabel on every single view controller managed view with the class, nib or storyboard name that is used. Great for debugging old or inherited projects with minefield architectures. It uses some cool libobjc runtime techniques to accomplish this, but implementing the category is a case of dropping it into your project and Build+Go!

Category in Action

Grab the source code here as usual, follow me @italoarmstrong on twitter 🙂

 

 

Overclocking & Optimising the Raspberry PI

My Raspberry PI

Out the box the Raspberry PI comes with a ARM1176JZFS Core (armv6 with hard float aka armhf arch) running at 700 Mhz as part of the Broadcom SoC. Additionally the memory frequency is also limited. In recent firmwares however… tinkerers have had the ability to “overclock” the Raspberry PI to squeeze some extra juice out of it. Mine’s currently running at 1Ghz at a solid 48C temperature when under load. So the first question that springs to mind is… why doesn’t everyone overclock their Raspberry PI? Well… there have been (well founded) reports of SD card corruption, heat/power issues and instability. The idea of this post is to show the user how to safety squeeze every last bit, cycle and IOP out of their PI safely’ish and without being an astrophysicist. Read on for the know-how. … 

 

How to Unlock almost any iPhone without any jailbreak or accessory

So… some of you may have already seen this, if you didn’t then, here it is 🙂 its pretty cool, it works… but has one caveat… Its a TEMPORARY unlock… i.e. it will unlock your phone sure, but once your TMSI (Temporary Mobile Subscriber Identity) is refreshed, you will have to repeat the process again… in the UK and US i think this happens when you switch the phone on/off or travel over a large geographic area… however it could potentially happen whenever…

Instructions on how to unlock are after the break 🙂

  1. Grab your iPhone
  2. Insert a supported Sim Card… so, if your phone is locked to Tmobile, put a Tmobile sim in there … 
 

Amazon Support Store

Hi everyone! I keep getting lots of emails from people asking where they can buy xyz to complete the tutorials and try out some of the things listed on CaptainGeek, well after I kept emailing people the same links i had a thought, why not setup an amazon affiliate store. Basically, i’ve setup a small amazon site with a small selection of products (only those used for the tutorials on this site + related ones), purchases and payments are handled by amazon, however a small percentage of the sale goes to helping fund the server this website is hosted on AT NO EXTRA COST TO YOU 🙂 so its a win win situation, please use the links whenever you can.

Our Amazon Store

 

Legal & Professional Issues In Computing

Well in about 30 minutes i’m going to sit an exam in this topic, covering the Data Protection Act (1998), Human Rights Act (1988), CAN Spam Act (2003), Computer Misuse Act (1990), Freedom of Information Act (2000) + many more. The subject material is actually quite interesting and enligtening,  i can say it has actually had an affect on my business and personal practise on the Internet. I’ll let you know how it goes later on…

Did you know: if someone hacks into your company computer, not only are they guilty under the Computer Misuse Act, you are also guilty for allowing it to happen under the Data Protection Act!

Update

Well it went rather well I think, it consisted of 50% multiple choice and 50% an essay style scenario. Mainly focusing around UK law, about 25% of the module was covered in the exam; a lot of wasted revision, but on the whole i think a success! =)