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:

  1.  
  2. public interface ISlave {
  3.  
  4.     function doSthg():String;
  5.     function order(command:String):void;
  6.     function answer():String;
  7.  
  8. }

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:

  1. slave.order("doSthg!");
  2.            
  3. verify().that(slave.answer());
  4. 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):

  1. var slave:ISlave = mock(ISlave) as ISlave;
  2.          
  3. given(slave.doSthg()).willReturn("done!");
  4. 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!):

  1. var answer:Answer = new GenericAnswer(
  2.     function():void {
  3.         trace(slave.answer());
  4.         trace(slave.doSthg());
  5.     }
  6. );
  7. 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:

  1. var slave:ISlave = mock(ISlave) as ISlave;
  2.          
  3. given(slave.doSthg()).willReturn("done!");
  4. given(slave.answer()).willReturn("yessir!");
  5.  
  6. var answer:Answer = new GenericAnswer(
  7.     function():void {
  8.         trace(slave.answer());
  9.         trace(slave.doSthg());
  10.     }
  11. );
  12. given(slave.order(eq("doSthg!"))).will(answer);
  13.          
  14. slave.order("doSthg!");
  15.            
  16. verify().that(slave.answer());
  17. 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!

This website uses IntenseDebate comments, but they are not currently loaded because either your browser doesn't support JavaScript, or they didn't load fast enough.

No Comments

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment