Skip to content

Mockito

BrianGenisio edited this page Apr 5, 2011 · 1 revision

Currently, Jasmine Spies only work on anonymous functions. If you want to use something like Mockito with Jasmine, it is quite easy.

Put the Mockito-v.swc in your libs folder with Jasmine-Flex and define this helper some place:

public class Prepare
{
	public static function Mockito(classes:Array, whenReady:Function):void
	{
		var complete:Boolean = false;
		
		currentMockito = currentMockito || new org.mockito.Mockito();
		currentMockito.prepareClasses(classes, function():void { complete = true });
		
		waitsFor(function():Boolean { return complete; });
		
		runs(function():void { whenReady(); });
	}
}

Now, you can prepare your mocked classes or interfaces in the beforeEach block:

beforeEach(function() {
	Prepare.Mockito([IReader], function() { 
		mockReader =  mock(IReader) as IReader;
		consumer = new Consumer(mockReader);
	});
});

And you can setup stubs and verifications as you need:

it("should read the reader", function() {
    // You can set up stubbed return values
    given(mockReader.read()).willReturn("something");
   
    consumer.doSomething();

    // you can validate read() was called
    verify().that(mockReader.read());
});