What’s the point of all this mockery?

I’ve jumped in with both feet, without pausing to explain why I’m doing it – so, where do mock objects fit into my daily testing behaviours?

  • when the system under test relies on an expensive resource
  • when the system under test relies on a live resource
  • when the system under test relies on a resource that doesn’t yet exist
  • when I want to ensure that the system under test uses the resource correctly

I had to build a couple of client applications recently (twitter and flickr, since you ask – talk about re-inventing the wheel …), which interacted with what I consider expensive resources – firstly, I was contacting the remote flickr servers and awaiting their response each time I tested; secondly, the twitter API puts limits on how often it’s called, which means that I could run out of calls pretty quickly if every test made at least one call, and all tests were run each time I compiled the movie.

A couple of years ago, we built a googlemaps-stylee flash app for a certain weekend-break company to allow users to choose chalets (/lodges/huts/accommodation units – bizarrely ‘accommodation units’ was the official term) online. The units’ availability was updated in real time, so the app had to query the server for the latest data. The only problem with this was that the server application was still under design – it didn’t yet exist.

I’ve also worked with database developers who haven’t yet finalised their DB structure, and who keep dropping and re-building the database, which means deleting all the data that my test scripts rely on.

You only need to experience difficulties such as these a couple of times to realise that it’s imperative to have control of the remote/live/non-existent/under-development resource in order for the tests to be of any use.

The good news is that – in most cases – it’s possible to define an API for communication with the resource even before it exists, and that’s what makes mock objects so useful – they can honour the API, thus enabling the code we build against the tests to work against the resource when it’s ready.

Strikes me an example might be useful. Twitter suit you?

I have a TwitterView object, which takes a TwitterProxy which in turn communicates with the Twitter servers. I know it’s terrible – aren’t these chaps supposed to be cleanly separated? The TwitterView receives an event when the proxy has done all its loading, and then can populate the relevant TextFields. Easy, innit.

public class TwitterView extends Sprite {

        private var title:TextField;
        private var tweets:TextField;
        private var proxy:ITwitterProxy;

        public function TwitterView(proxy:ITwitterProxy = null) {
            this.proxy = proxy;
            initView();
            setTitle("Hello World!");
        }

        public function getTitle():String {
            return title.text;
        }

        public function setTitle(title:String):void {
            this.title.text = title;
        }

        public function setTweets(tweets:Array):void {
            this.tweets.htmlText = tweets.join("\n\n");
        }

        override public function toString():String {
            return "[TwitterView]";
        }

        public function init():void {
            if(proxy == null) {
                proxy = new TwitterProxy();
            }
            proxy.addEventListener(TwitterLoadEvent.TWITTER_LOADED, onTwitterLoaded);
            proxy.init();
        }

        private function initView():void {
            title = new TextField();
            title.autoSize = TextFieldAutoSize.LEFT;
            addChild(title);

            tweets = new TextField;
            tweets.width = 500;
            tweets.autoSize = TextFieldAutoSize.LEFT;
            tweets.wordWrap = true;
            tweets.y = title.y + title.height + 10;
            addChild(tweets);
        }

        public function onTwitterLoaded(event:TwitterLoadEvent):void {
            setTitle(event.proxy.getTitle());
            setTweets(event.proxy.getTweets());
        }
    }

In the onTwitterLoaded function, you can see the two most important calls that we need to make on the TwitterProxy: getTitle() and getTweets(). I put these in the ITwitterProxy interface.

public interface ITwitterProxy extends IEventDispatcher {

        function getTweets():Array;
        function getTitle():String;

        function init():void;
    }

So now we have a fairly basic view, which expects its data from the proxy.

Let’s see it in action, courtesy of TwitterApp. Yep, not much happening:

public class TwitterApp extends Sprite {

        public function TwitterApp() {
            init();
        }

        private function init():void {
            var view:TwitterView = new TwitterView();
            addChild(view);
            view.init();
        }
    }

We want to practise test-driven development – write a failing test > code until it passes > write another test > refactor – so we want to write a unit test that will check the view’s title and tweets, but (a) we can’t rely on the live twitter data as it will change over time, and (b) we don’t want to run up against the limit of API calls that we can make. Time for a mock object, methinks.

Firstly, I’m using ASUnit rather than FlexUnit, so if you’re following along, you’ll need to grab that and the ASUnitMockitoTestCase bridging class that I published one or two articles ago. Let’s start with a skeleton testcase:

public class TwitterTest extends ASUnitMockitoTestCase {

        public function TwitterTest(testMethod:String = null) {
            super([], testMethod);
        }

        public function testBasic():void {
            assertTrue("failing test", 1 + 1 == 5);
        }

}

Notice that we have a basic test that will show up as a fail in ASUnit, so that we can be sure that we’ve hooked everything together successfully. As soon as you see the fail, you can delete it. Note also that the call to super() starts with an empty Array – in time we’ll populate with the classes that we wish to set up as mocks.

Now, let’s pause for a think about what we want to test here – the View relies on the proxy for its data, so we want to check that the view gets its data successfully from the proxy without hitting the live Twitter servers. Oh, and the proxy doesn’t exist yet, just an interface.

I’m going to start really slowly here, forgive me – adding the assert that checks the view’s title.

public class TwitterTest extends ASUnitMockitoTestCase {

        public function TwitterTest(testMethod:String = null) {
            super([], testMethod);
        }

        public function testViewGetTitle():void {
            assertEquals("Twitter updates", view.getTitle());
        }
}

Immediate compile error, since we haven’t declared view, so let’s add another line:

var view:TwitterView = new TwitterView();
assertEquals("Twitter updates", view.getTitle());

Still no success, because the TwitterView cannot be instantiated without a ITwitterProxy, which we don’t have, so let’s mock that (note that I’ve also added ITwitterProxy to the super() in the TwitterTest() constructor).

public class TwitterTest extends ASUnitMockitoTestCase {

        public function TwitterTest(testMethod:String = null) {
            super([ITwitterProxy], testMethod);
        }

        public function testViewGetTitle():void {
            var mockProxy:ITwitterProxy = mock(ITwitterProxy) as ITwitterProxy;
            var view:TwitterView = new TwitterView(mockProxy);
            view.init();
            assertEquals("Twitter updates", view.getTitle());
        }
}

Huzzah – the movie compiles and we get our failing test :) For the record, mock() takes the interface and builds an object based on it that will record all calls made on its methods – it’s very, very clever.

First thing, let’s set the mockProxy up to give us the title string that we expect – add this line above the creation of the view:

given(mockProxy.getTitle()).willReturn("Twitter updates");

This beautiful line of code says: if someone calls getTitle() on the mockProxy, the mockProxy will return “Twitter updates”. Isn’t that cool? Of course, that doesn’t help pass the test just yet.

The view is expecting to receive a TwitterLoadEvent from the proxy, triggering onTwitterLoaded(), so our mock object needs to have IEventDispatcher functionality. However, because the mockObject does so much weird stuff behind the scenes that I don’t have (and don’t want to have) a clue about, I’m going to attack this another way.

given(mockProxy.addEventListener(any(), any())).will(fireImmediateLoadEvent);

When mockProxy.addEventListener() is called, it will fire an immediate load event – this is mocking the request/response communication with the Twitter server. So what’s fireImmediateLoadEvent?

It’s a GenericAnswer object, which just holds a function that will be called when addEventListener() is called; in this case I want it to be like this:

var fireImmediateLoadEvent:Answer = new GenericAnswer(
    function():void {
        // record the event and eventHandler somehow
        // then immediately fire the event with the required data
    }
);

Of course, in the real world, the proxy will be doing this as part of its work, but because we’re using a mock proxy, we have to work around it a bit. I’ve come up with this:

var d:EventDispatcher = new EventDispatcher({} as IEventDispatcher);

var fireImmediateLoadEvent:Answer = new GenericAnswer(
    function():void {
        // record the event and eventHandler somehow
        d.addEventListener(TwitterLoadEvent.TWITTER_LOADED, view.onTwitterLoaded);
        // then immediately fire the event with the required data
        d.dispatchEvent(new TwitterLoadEvent(mockProxy));
    }
);

So now the test looks like this:

    public function testViewGetTitle():void {
        // create an EventDispatcher that can be used as the dispatching functionality for
        // the mock ITwitterClient
        var d:EventDispatcher = new EventDispatcher({} as IEventDispatcher);

        var mockProxy:ITwitterProxy = mock(ITwitterProxy) as ITwitterProxy;
        given(mockProxy.getTitle()).willReturn("Twitter updates");
        given(mockProxy.getTweets()).willReturn(["Tweet#1", "Tweet#2", "Tweet#3"]);

        var view:TwitterView = new TwitterView(mockProxy);
        var fireImmediateLoadEvent:Answer = new GenericAnswer(
            function():void {
                // add the listening class
                d.addEventListener(TwitterLoadEvent.TWITTER_LOADED, view.onTwitterLoaded);
                // then immediately fire the event - this mocks the XML-loading that really occurs
                d.dispatchEvent(new TwitterLoadEvent(mockProxy));
            }
        );
        given(mockProxy.addEventListener(any(), any())).will(fireImmediateLoadEvent);
        view.init();

        assertEquals("Twitter updates", view.getTitle());
    }

For completeness, here’s TwitterLoadEvent:

public class TwitterLoadEvent extends Event {

    public static const TWITTER_LOADED:String = "onTwitterLoaded";

    private var _proxy:ITwitterProxy;

    public function TwitterLoadEvent(proxy:ITwitterProxy) {
        super(TWITTER_LOADED, false, false);
        _proxy = proxy;
    }

    public function get proxy():ITwitterProxy {
        return _proxy;
    }
}
Posted in Development, Technology | Tagged , , , , | Leave a comment

Mocking slavery

New to the idea of such mockery, I was very lucky to find a tutorial using the Java version which I could convert as I worked through it.

Now I’m converted – it’s a very neat and fast way of stubbing out the behaviours that I wish to code.

This morning, however, I came across a slightly trickier case to mock – at least, a case that required trial and error, as it hasn’t been documented that fully.

I have a slave interface:


public interface ISlave {

    function doSthg():String;
    function order(command:String):void;
    function answer():String;

}

And the behaviour I want is that when a slave is ordered to “doSthg!”, it should answer() and doSthg() pretty sharpish. So the test looks like this:

slave.order("doSthg!");

verify().that(slave.answer());
verify().that(slave.doSthg());

To verify().that(slave.answer()) means to verify that slave.answer was called once.

First off, I needed to define the doSthg() and answer() methods, having created a mock slave from the interface (both return a String object):

var slave:ISlave = mock(ISlave) as ISlave;

given(slave.doSthg()).willReturn("done!");
given(slave.answer()).willReturn("yessir!");

And then I came to the tricky bit – since I’m creating this object out of an Interface definition how can I define the behaviour of slave.order()?

Well, thankfully the GenericAnswer object literally holds the answer to that question – it is instantiated with a function thus and given to the mock slave.order (if the call parameter is correct!):

var answer:Answer = new GenericAnswer(
    function():void {
        trace(slave.answer());
        trace(slave.doSthg());
    }
);
given(slave.order(eq("doSthg!"))).will(answer);

So now I have created an ISlave interface with three public methods, and defined how the methods interact, and precisely what parameter in order() will trigger the correct interaction. In full it looks like this:

var slave:ISlave = mock(ISlave) as ISlave;

given(slave.doSthg()).willReturn("done!");
given(slave.answer()).willReturn("yessir!");

var answer:Answer = new GenericAnswer(
    function():void {
        trace(slave.answer());
        trace(slave.doSthg());
    }
);
given(slave.order(eq("doSthg!"))).will(answer);

slave.order("doSthg!");

verify().that(slave.answer());
verify().that(slave.doSthg());

P.S. Obviously, the slave should not have a order() method – that’s the responsibility of the master (e.g. master.order(slave, “doSthg!”)), but it serves to make this example work, so forgive me!

Posted in Development | Tagged , , , | Leave a comment

Mockito-Flex meets ASUnit

Well, that’s not a catchy headline, but it does pretty much sum it up. I’ve been playing with Mockito-Flex, a mock object framework for Actionscript 3. It’s great, and made vastly easier to pick up by a Mockito-FlexUnit bridge in the form of a MockitoTestCase that does the hard work of reporting the mocking results and validations in a way that FlexUnit can display.

Of course, FlexUnit does require the Flex framework, which bulks up the whole thing. Oh, and I’m used to using ASUnit. So I’ve written the following class that does the same job as MockitoTestCase, but for ASUnit:

/**
 * The MIT License
 *
 * Copyright (c) 2009 Mockito contributors
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
 * and associated documentation files (the "Software"), to deal in the Software without restriction,
 * including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
 * AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

package org.mockito {
    import asunit.framework.TestCase;

    import org.mockito.api.Matcher;
    import org.mockito.api.MethodSelector;
    import org.mockito.api.MockCreator;
    import org.mockito.api.Stubber;
    import org.mockito.api.Verifier;

    public class ASUnitMockitoTestCase extends TestCase {

        private var _mockClasses:Array;

        protected var mockito:Mockito;

        public function ASUnitMockitoTestCase(mockClasses:Array, testMethod:String = null)
        {
            _mockClasses = mockClasses;
            super(testMethod);
        }

        /**
         * Due to the asynchronous nature of the class generation
         * a test needs to execute from a callback function
         */
        public override function run():void
        {
            if (mockito == null && _mockClasses)
            {
                mockito = new Mockito();
                var superRun:Function = super.run;
                mockito.prepareClasses(_mockClasses, repositoryPreparedHandler);
                function repositoryPreparedHandler():void
                {
                    superRun();
                }
            }
            else
            {
                super.run();
            }
        }

        /**
         * Constructs mock object
         * @param clazz a class of the mock object
         * @param constructorArgs constructor arguments required to create mock instance
         * @param name a name used in various output
         * @return a mocked object
         */
        public function mock(classToMock:Class, name:String = null, constructorArgs:Array = null):Object
        {
            return mockito.mock(classToMock, name, constructorArgs);
        }

        /**
         * A starter function for verification of executions
         * If you dont specify the verifier, an equivalent of times(1) is used.
         * @param verifier object responsible for verification of the following execution
         */
        public function verify(verifier:Verifier = null):MethodSelector
        {
            return mockito.verify(verifier);
        }

        /**
         * A starter function for stubbing
         * @param methodCallToStub call a method to stub as an argument
         * @return an object providing stubbing options
         */
        public function given(methodCallToStub:*):Stubber
        {
            return mockito.given(methodCallToStub);
        }

        /**
         * @private
         */
        protected function get mockCreator():MockCreator
        {
            return mockito;
        }

        /**
         * Matches any argument including null
         */
        public function any():*
        {
            return mockito.any();
        }

        /**
         * Equality matcher
         * Example:
         *
         * verify(never()).that(system.login(eq("root")));
         * 
         */
        public function eq(expected:*):*
        {
            return mockito.eq(expected);
        }

        /**
         * A fluent interface for making sure call hasn't happened
         * Example:
         *
         * verify(never()).that(operator.execute());
         * 
         */
        public function never():Verifier
        {
            return mockito.never();
        }

        /**
         * A fluent interface for counting calls
         * Example:
         *
         * verify(times(2)).that(operator.execute());
         * 
         */
        public function times(expectedCallsCount:int):Verifier
        {
            return mockito.times(expectedCallsCount);
        }

        /**
         * A fluent interface for custom matcher
         * Example:
         *
         * verify().that(system.login(argThat(new HashOnlyCapitalLettersMatcher())));
         * 
         *
         * A good practice is to create a matcher recording function somewhere and name it
         * after the matcher. It's important to return a wildcard from the function to let it
         * work with any arugment of the function
         *
         * function hasOnlyCapitalLetters():*
         * {
         *     argThat(new HashOnlyCapitalLettersMatcher());
         * }
         * 
         */
        public function argThat(matcher:Matcher):*
        {
            return mockito.argThat(matcher);
        }
    }
}

I’ve built this against ASUnit3, and it appears to be running fine. As you can see, it’s release under the MIT license, as is Mockito-Flex. I’d appreciate any feedback that’s going, so have a play and let me know …

Posted in Development | Tagged , , , | 2 Comments

Rocks into Gold

Clarke Ching – whose work I’ve been reading for a while, is preparing to publish a short parable for these troubled times. Get in touch with him via this post, and grab a copy, after all the more weapons in our armoury, the better chance we have of winning the inevitable battles.

Posted in management, process | Tagged , , , , | Leave a comment

I want problems, not solutions!

So, there I was, quietly listening into a conference call between a couple of clients on one side, and the account manager, project manager and me on the other.

We were going through a small project that we’d just completed for the purposes of getting sign-off. We had built in the functionality they wanted, using the designs that they’d agreed to, so it was plain sailing.

And then the spanner.

Continue reading

Posted in management, process | Tagged , , , | Leave a comment

Testing Proxies in PureMVC

This post is prompted by Larry Marburger’s article, since I came across this problem a few weeks ago, and found a different solution.

The scenario
There’s this great new framework that you’re starting to use, and it’s persuaded you to turn a new leaf and unit-test your work as you go. First off, be warned this is all AS2.0, using asunit2.5, because I’m still stuck in the dark ages :(

The problem
PureMVC uses Proxy objects to access data from the model, which fire off Notifications (PureMVC-specific events) when the data is ready. However, in the case of a Proxy that loads XML before making its data available, how do we know when it’s ready to be tested?

Continue reading

Posted in Development, puremvc | Tagged , , , | 4 Comments

A grown-up conversation

Several places I’ve worked have spoken about the client in two contrasting ways: one, hushed tones suggesting that they’re too sensitive to handle whatever reality we’re dealing with; two, derision suggesting that they’re too much of an idiot to understand whatever reality we’re dealing with.

Both are clearly untrue (to greater or lesser degrees), and both serve to make our jobs harder because they distance us from the most important person in any project.

So here’s a list of questions I would like answered:

  • why do we keep secrets from clients?
  • why are we so keen to promise tight deadlines?
  • why do we try to squeeze in extra stuff for them?
  • what if we treat them as part of the team?
  • what if we made them fully aware of the repercussions of their decisions?
  • what if we made demands of them, in order to deliver
  • what if we explain how risk builds up, and what they can do to mitigate it?

Continue reading

Posted in management, process | Tagged , | 5 Comments

Project in distress (IV)

I particularly like this one: a week has passed, whilst the project manager has been “working” with the designer on the definition of 3D behaviour of playing cards on the site. His output landed in my in box yesterday afternoon: it was a list of cards, and the places they would sit in on the site.

This information was available in the original pitch document! So has it really taken a week to change the document format?

Posted in management, process | Tagged | Leave a comment

Finding out what’s best …

Just doing our best ain’t good enough – we need to be more savvy than that and work out precisely what’s the best thing to do first. So far, we’ve identified the conflict, and we now need to assess possible solutions.

Continue reading

Posted in management, process | Tagged , , , | 2 Comments

Project in distress (III)

It’s Monday, so I’m back with fresh energy. Soon sapped by the realisation that if we ballpark 2 months for development (excluding testing/bugfixing), we need to start at the beginning of June. Which means the designs have to be finalised and signed-off within 3 weeks, and we haven’t even agreed all the content for the site yet.

Here’s a twist I haven’t come across before – the site is companion to a quarterly members’ magazine, so the design has to cover both versions. As a result, the print designers of the magazine get to take the lead on the look’n'feel and design, and once they’re happy hand it off to the digital designer. All within 3 weeks obviously.

None of the designers have worked with me before, so this’ll be double fun.

Oh, and there’s a “secret” area of the site for magazine subscribers. So secret, apparently, that we aren’t yet privy to what will go in it.

Posted in Development, management, process | Tagged | Leave a comment