reduce overall energy expenditure. It makes you write labour saving programs that other people find useful, and document what you wrote so that you don’t have to answer so many questions about it. LAZINESS
(); var container:Container = new Container(); container.addChild(component); assertThat(component.invalidatePropertiesWasCalled, isTrue()); } internal class UIComponentForTesting extends UIComponent { public var invalidatePropertiesWasCalled:Boolean; override public function invalidateProperties():void { invalidatePropertiesWasCalled = true; super.invalidateProperties(); } }
var entity:IEntity = new Entity(); var cmd:SaveEntityCommand = new SaveEntityCommand(); cmd.entityManager = entityManager; cmd.entity = entity; cmd.execute(); } class FakeEntityManager implements IEntityManager { public function save(entity:IEntity):Boolean { if (entity.isModified) { entity.updated(); return true; } return false; } }
[Test] public function with_unmodified_entity_should_disable_saving():void { presenter.setEntity(new UnmodifiedEntityStub()); assertThat(presenter.saveEnabled, isFalse()); } internal class ModifiedEntityStub extends Entity { override public function isModified():Boolean { return true; } } internal class UnmodifiedEntityStub extends Entity { override public function isModified():Boolean { return false; } }
target.methodThatTriggersEvents(); assertThat(target.dispatcher.numEventsDispatched, equalTo(0)); } internal class SpyingEventDispatcher extends EventDispatcher { private var _numEventsDispatched:int = 0; public function get numEventsDispatched():int { return _numEventsDispatched } override public function dispatchEvent(event:Event):Boolean { _numEventsDispatched++; super.dispatchEvent(event); } }
{ return new Result(); } } } package mockolate.examples.generated { public class ExampleProxy extends Example { private var _interceptor:Interceptor public function ExampleProxy(interceptor:Interceptor) { _interceptor = interceptor; } override public function method(...args):Result { return _interceptor.intercept( new Invocation(this, "method", args, super.method)); } } }
mocks:MockolateRule = new MockolateRule(); public var kettle:Kettle; [Mock] public var heatingElement:HeatingElement; [Mock] public var thermostat:Thermostat; [Before] public function prepareKettle():void { kettle = new Kettle(heatingElement, thermostat); } } }
mocks:MockolateRule; [Mock] public var heatingElement:HeatingElement; [Mock] public var thermostat:Thermostat; [Mock] public var dispatcher:EventDispatcher; } }
mocks:MockolateRule; [Mock(type=”nice”)] public var heatingElement:HeatingElement; [Mock(type=”strict”)] public var thermostat:Thermostat; [Mock(type=”partial”)] public var dispatcher:EventDispatcher; } }
public function switching_kettle_off_should_deactivate_thermostat ():void { mocks.expect(thermostat.deactivate()).once(); kettle.switchOn(); kettle.switchOff(); }
mocks:MockolateRule = new MockolateRule(); public var kettle:Kettle; [Mock] public var heatingElement:HeatingElement; [Mock] public var thermostat:Thermostat; [Before] public function prepareKettle():void { kettle = new Kettle(heatingElement, thermostat); } } }
about things that should not happen Specify as little as possible in a test Don’t use mocks to test boundary objects Don’t add behaviour Only mock your immediate neighbours Avoid too many mocks Create objects indirectly