SlideShare a Scribd company logo
Designing Testable Code
by Bjorn Schultheiss




                       Topic                     Presenter
                       Designing Testable Code   Bjorn Schultheiss
Who am I?
Bjorn Schultheiss
Developer
Adslot


Front end developer
Flash, Flex and now even some Javascript
Large projects including http://www.adchamp.com




                      Topic                       Presenter
                     Designing Testable Code      Bjorn Schultheiss
Recent thoughts about Flash




       Topic                     Presenter
       Designing Testable Code   Bjorn Schultheiss
Find your passion
       Topic
       Designing Testable Code
                                 Presenter
                                 Bjorn Schultheiss
Testable code you say
This presentation will cover


  • How to look at OO
  • Static methods and Global State
  • Separation of object construction and logic
  • Constructor Injection vs Setter Injection
  • Presentation Model

                        Topic                     Presenter
                        Designing Testable Code   Bjorn Schultheiss
Sorry I’m busy dude
       Topic
       Designing Testable Code
                                 Presenter
                                 Bjorn Schultheiss
And why would I be interested?
This presentation is targeted at


  • Any developer creating heavy client side applications
  • Focused specifically on Flex but the same principals
       apply to other web languages and frameworks

  • you love that feeling from watching a test pass


                        Topic                     Presenter
                        Designing Testable Code   Bjorn Schultheiss
Tell me about Flex Unit
This talk is not about Flexunit


  • Flex developer’s best friend
  • Created by the community
  • Endorsed by Adobe (integrated in FB)
  • Easy to get started with


                         Topic                     Presenter
                         Designing Testable Code   Bjorn Schultheiss
Topic                     Presenter
Designing Testable Code   Bjorn Schultheiss
What are unit tests?




        Topic                     Presenter
        Designing Testable Code   Bjorn Schultheiss
I knew you cowboys were here
       Topic
       Designing Testable Code
                                 Presenter
                                 Bjorn Schultheiss
• They are written by software engineers, not test
  engineers.

• They are better written as you code, not after you’ve
  finished




               Topic                     Presenter
               Designing Testable Code   Bjorn Schultheiss
The only good excuse not to be writing them is because
you don’t know how.




               Topic                     Presenter
               Designing Testable Code   Bjorn Schultheiss
So what do i need to know?




       Topic                     Presenter
       Designing Testable Code   Bjorn Schultheiss
How to think about OO

• OO is about the relationship between data and
  code

• data represents the state of an object
• code modifies that state


              Topic                     Presenter
              Designing Testable Code   Bjorn Schultheiss
Procedural

	   public class ImageEditor
	   {
	   	
	   	 public function crop(image:Bitmap, size:Rectangle):Bitmap
	   	 {
	   	 	 // do something
	   	 }




                     Topic                         Presenter
                     Designing Testable Code       Bjorn Schultheiss
Better
 public   class ImageEditor
 	 {
 	 	      private var _image:Bitmap;
 	 	
 	 	      public function ImageEditor(image:Bitmap) {
 	 	      	 _image = image;
 	 	      }
 	 	
 	 	      public function crop(size:Rectangle):void
 	 	      {
 	 	      	 // do something
 	 	      }
 	 }




                      Topic                             Presenter
                     Designing Testable Code          Bjorn Schultheiss
Global State

• Object state is subject to garbage collection
• Global state is subject to life of the session




              Topic                     Presenter
              Designing Testable Code   Bjorn Schultheiss
Global State
a = new X() -->




                  Topic                     Presenter
                  Designing Testable Code   Bjorn Schultheiss
Global State
                                 Y
a = new X() -->                                         Z
                          X
                                            Q




                  Topic                     Presenter
                  Designing Testable Code   Bjorn Schultheiss
Global State
                                 Y
a = new X() -->                                         Z
                          X
                                            Q



b = new X() -->




                  Topic                     Presenter
                  Designing Testable Code   Bjorn Schultheiss
Global State
                                 Y
a = new X() -->                                         Z
                          X
                                            Q



                                 Y
b = new X() -->                                          Z
                          X
                                            Q

                  Topic                     Presenter
                  Designing Testable Code   Bjorn Schultheiss
Global State
                                  Y
a = new X() -->                                          Z
a.doSomething();           X
                                             Q



                                  Y
b = new X() -->                                           Z
                           X
                                             Q

                   Topic                     Presenter
                   Designing Testable Code   Bjorn Schultheiss
Global State
                                  Y
a = new X() -->                                          Z
a.doSomething();           X
                                             Q



                                  Y
b = new X() -->                                           Z
b.doSomething();           X
                                             Q

                   Topic                     Presenter
                   Designing Testable Code   Bjorn Schultheiss
Global State
                                  Y
a = new X() -->                                          Z
a.doSomething();           X
                                             Q
a == b
                                  Y
b = new X() -->                                           Z
b.doSomething();           X
                                             Q

                   Topic                     Presenter
                   Designing Testable Code   Bjorn Schultheiss
Global State
                                  Y
a = new X() -->                                          Z
a.doSomething();           X
                                             Q
                                                                 GS
a == b
                                  Y
b = new X() -->                                           Z
b.doSomething();           X
                                             Q

                   Topic                     Presenter
                   Designing Testable Code   Bjorn Schultheiss
Global State

• Inconsistent results
• Order of tests matter
• Something the tester doesn’t control



             Topic                     Presenter
             Designing Testable Code   Bjorn Schultheiss
Singletons




       Topic                     Presenter
       Designing Testable Code   Bjorn Schultheiss
Singleton’s are pathological liars
         Topic
         Designing Testable Code
                                   Presenter
                                   Bjorn Schultheiss
Singletons
• Singletons use global state




             Topic                     Presenter
             Designing Testable Code   Bjorn Schultheiss
package
{
	   public class AppSettings
	   {
	   	   private static const _instance:AppSettings = new AppSettings(Lock);
	   	
	   	   private var _state1:Object;
	   	   private var _state2:Object;
	   	   private var _state3:Object;
	   	
	   	   public static function get instance():AppSettings {
	   	   	   return _instance;
	   	   }
	   	
	   	   public function AppSettings(lock:Class) {
	   	   	   if (lock!=Lock) throw new Error("cannot unlock");
	   	   }
	   }
}

internal class Lock {}




                 Topic                              Presenter
                 Designing Testable Code            Bjorn Schultheiss
Singletons
• Singletons use global state
• the test doesn’t control the instantion of the
   process




              Topic                     Presenter
              Designing Testable Code   Bjorn Schultheiss
package
{
	   public class AppSettings
	   {
	   	   private static const _instance:AppSettings = new AppSettings(Lock);
	   	
	   	   private var _state1:Object;
	   	   private var _state2:Object;
	   	   private var _state3:Object;
	   	
	   	   public static function get instance():AppSettings {
	   	   	   return _instance;
	   	   }
	   	
	   	   public function AppSettings(lock:Class) {
	   	   	   if (lock!=Lock) throw new Error("cannot unlock");
	   	   }
	   }
}

internal class Lock {}




                 Topic                              Presenter
                 Designing Testable Code            Bjorn Schultheiss
Singletons
• Singletons use global state
• the test doesn’t control the instantion of the
   process

• secret collaborators



              Topic                     Presenter
              Designing Testable Code   Bjorn Schultheiss
public class CreditCard
	   {
	   	   private var _gateway:PaymentGateway;
	   	
	   	   public function CreditCard()
	   	   {
	   	   	   gateway = ServiceLocator.getInstance().gateway;
	   	   }
	   	
	   	   public function chargeCard(value:Number):void
	   	   {
	   	   	   gateway.performTransaction(value);
	   	   }
	   }




	   var cc:CreditCard = new CreditCard();
	   cc.chargeCard(10);




                      Topic                              Presenter
                     Designing Testable Code             Bjorn Schultheiss
Singletons
• Singletons use global state
• the test doesn’t control the instantion of the
   process

• secret collaborators
• you may know the dependencies, but anyone that
   comes after you is baffled!



              Topic                     Presenter
              Designing Testable Code   Bjorn Schultheiss
Law of Demeter

Object.getSomething.theProperty




              Topic                     Presenter
              Designing Testable Code   Bjorn Schultheiss
Law of Demeter

Object.getSomething.theProperty




              Topic                     Presenter
              Designing Testable Code   Bjorn Schultheiss
Law of Demeter

Object.getSomething.theProperty

something.theProperty




              Topic                     Presenter
              Designing Testable Code   Bjorn Schultheiss
Locator in action
 class   House {
 	 	
 	 	
 	 	
 	 	
 	 	     public function House(locator:Locator) {
 	 	     	
 	 	     	 // what needs to be mocked in test?
 	 	     	
 	 	     }
 	 }




                     Topic                          Presenter
                     Designing Testable Code        Bjorn Schultheiss
Use revealed
 class   House {
 	 	      private var _door:Door;
 	 	      private var _roof:Roof;
 	 	      private var _window:Window
 	 	
 	 	     public function House(locator:Locator) {          API gave us
 	 	     	 _door = locator.getDoor();
 	 	     	 _roof = locator.getRoof();                        no help
 	 	     	 _window = locator.getWindow();
 	 	     }
 	 }




                      Topic                         Presenter
                      Designing Testable Code       Bjorn Schultheiss
An API that helps
	   class House {
	   	 private var _door:Door;
	   	 private var _roof:Roof;
	   	 private var _window:Window
	   	
	   	 public function House(door:Door, roof:Roof, window:Window) {
	   	 	 _door = door;
	   	 	 _roof = roof;
	   	 	 _window = window;
	   	 }
	   }




                      Topic                         Presenter
                     Designing Testable Code        Bjorn Schultheiss
Similar Anti-Patterns
I didn’t ask for that!!


  • aka Locator
  • aka Context
  • aka Manager
  • Hides true dependencies
  • Breaks law of demeter

                          Topic                     Presenter
                          Designing Testable Code   Bjorn Schultheiss
Constructor vs Setter injection

• An order of instantiation




              Topic                     Presenter
              Designing Testable Code   Bjorn Schultheiss
Setter example
<s:Group xmlns:fx="http://ns.adobe.com/mxml/2009"
	    	      xmlns:s="library://ns.adobe.com/flex/spark">
	    <fx:Script>
	    	    <![CDATA[
	    	    	     import mx.collections.ArrayCollection;
	    	    	     import mx.collections.IList;
	    	    	     import mx.rpc.AsyncToken;
	    	    	
	    	    	     public var userService:Service;
	    	    	
	    	    	     public function findUsers():void {
	    	    	     	    userService.findAll();
	    	    	     	    userService.addEventListener("onResult", onResult);
	    	    	     }
	    	    	
	    	    	     public function onResult(result:Object):void {
	    	    	     	    users.dataprovider = new ArrayCollection(result);
	    	    	     }
	    	    ]]>
	    </fx:Script>
	    <s:Button label="find all users" click="findUsers();" />
	    <s:List id="users" />
</s:Group>




                            Topic                                          Presenter
                            Designing Testable Code                        Bjorn Schultheiss
Constructor example
 public class ShowUserModel
 	    {
 	    	   [Bindable]
 	    	   public var users:IList;
 	    	   private var _service:Service;
 	    	
 	    	   public function ShowUserModel(service:Service)
 	    	   {
 	    	   	    _service = service;
 	    	   	    _service.addEventListener("onResult", onResult);
 	    	   }
 	    	
 	    	   public function findUsers():void
 	    	   {
 	    	   	    _service.findAll();
 	    	   }
 	    	
 	    	   private function onResult(result:Object):void {
 	    	   	    users = new ArrayCollection(result);
 	    	   }
 	    }




                           Topic                                  Presenter
                           Designing Testable Code                Bjorn Schultheiss
Instantiation example
	   	   [Test]
	   	   public function testShowUsersComponent():void
	   	   {
	   	   	   var service:Service = new Service("Users");
	   	   	   var component:ShowUsersComponent = new ShowUsersComponent();
	   	   	   component.service = service;
	   	   	   component.findUsers();
	   	   }
	   	

	   	   [Test]
	   	   public function testShowUsersModel():void
	   	   {
	   	   	   var service:Service = new Service("Users");
	   	   	   var model:ShowUsersModel = new ShowUsersModel(service);
	   	   	   model.findUsers();
	   	   }




                                Topic                                  Presenter
                                Designing Testable Code                Bjorn Schultheiss
How does this relate to unit
testing?




        Topic                     Presenter
        Designing Testable Code   Bjorn Schultheiss
How does this relate to unit
testing?

      Unit testing as the name
 implies you test a Class (unit)
                     in isolation

        Topic                     Presenter
        Designing Testable Code   Bjorn Schultheiss
If your class mixes Object
construction and logic you will
never be able to achieve
isolation


        Topic                     Presenter
        Designing Testable Code   Bjorn Schultheiss
In order to unit test you
    need to separate your
object graph construction
      from the logic into 2
          different classes

   Topic                     Presenter
   Designing Testable Code   Bjorn Schultheiss
The end goal is to have classes with either logic or “new”
operators




                Topic                     Presenter
                Designing Testable Code   Bjorn Schultheiss
Construction and logic mixed
 	   public class House
 	   {
 	   	   private var _kitchen:Kitchen = new Kitchen();
 	   	   private var _isLocked:Boolean;
 	   	
 	   	   public function get isLocked():Boolean {
 	   	   	   return _isLocked;
 	   	   }
 	   	
 	   	   public function lock() {
 	   	   	   _kitchen.lock();
 	   	   	   _isLocked = true;
 	   	   }
 	   }




                       Topic                             Presenter
                       Designing Testable Code           Bjorn Schultheiss
Injection example
 public class House
 	   {
 	   	    private var _kitchen:Kitchen;
 	   	    private var _isLocked:Boolean;
 	   	
 	   	    public function House(kitchen:Kitchen) {
 	   	    	   _kitchen = kitchen;
 	   	    }
 	   	
 	   	    public function get isLocked():Boolean {
 	   	    	   return _isLocked;
 	   	    }
 	   	
 	   	    public function lock() {
 	   	    	   _kitchen.lock();
 	   	    	   _isLocked = true;
 	   	    }
 	   }




                        Topic                        Presenter
                        Designing Testable Code      Bjorn Schultheiss
Construction example

  function build():House {
  	 	 	 return new House(new Kitchen(
  	 	 	 	 new Sink(),
  	 	 	 	 new Dishwasher(),
  	 	 	 	 new Refrigerator())
  	 	 	 );
  }




                 Topic                    Presenter
                Designing Testable Code   Bjorn Schultheiss
lets look at writing a unit test




         Topic                     Presenter
         Designing Testable Code   Bjorn Schultheiss
Writing a unit test
to test a method first you need to instantiate an object


  • Specifically the class or component to be tested
  • Any collaborators (real or mock).




                        Topic                             Presenter
                        Designing Testable Code           Bjorn Schultheiss
[Test]
	   	   public function testHouse():void
	   	   {
	   	   	 var kitchen:Kitchen = new Kitchen();
	   	   	 var house:House = new House(kitchen);
	   	   	 house.testMethod();
	   	   }




                    Topic                         Presenter
                    Designing Testable Code       Bjorn Schultheiss
Testing UI Components




       Topic                     Presenter
       Designing Testable Code   Bjorn Schultheiss
Spare me
       Topic
       Designing Testable Code
                                 Presenter
                                 Bjorn Schultheiss
Testing UI Components
Possible but generally a pain, why?


  • test a style is applied
  • mimic user interaction
  • logic is is mixed with view



                       Topic                     Presenter
                       Designing Testable Code   Bjorn Schultheiss
[Before(async,ui)]
public function setUp() : void
{
	   myButton = new MyButton();
	   Async.proceedOnEvent( this, myButton, FlexEvent.CREATION_COMPLETE, 100 );
	   UIImpersonator.addChild( myButton );
}
 
 
[Test(async,ui)]
public function myButton_myStyle_defaultValue() : void
{
	   Assert.assertEquals( myButton.getStyle( "myStyle" ), "expectedDefaultValue" );
}




                     Topic                              Presenter
                     Designing Testable Code            Bjorn Schultheiss
UI Codefendants
• UIImpersonator
• invalidationModel
• [Async] proceedOnEvent()
• displayList hierarchy



            Topic                     Presenter
            Designing Testable Code   Bjorn Schultheiss
Presentation Models
http://martinfowler.com/eaaDev/PresentationModel.html
Represent the state and behaviour of the presentation independently of the GUI controls
used in the interface


  • uses Binding to interact with view
  • also handles events from view
  • a pattern used in popular frameworks
  • great for unit testing

                       Topic                           Presenter
                       Designing Testable Code         Bjorn Schultheiss
Presentation Model example
<s:Group xmlns:fx="http://ns.adobe.com/mxml/2009">
                                                      package presentation
	    <fx:Script>
                                                      {
	    	    <![CDATA[
                                                      	    [Bindable]
	    	    	    import presentation.HelloWorldModel;
                                                      	    public class HelloWorldModel
	    	    	    [Bindable]
                                                      	    {
	    	    	    public var model:HelloWorldModel;
                                                      	    	    public var label:String = "Hello";
	    	    ]]>
                                                      	    	
	    </fx:Script>
                                                      	    	    public function clickHandler():void
	    <s:Label text="{ model.label }"
                                                      	    	    {
click="model.clickHandler();" />
                                                      	    	    	    trace("Is it me your looking for?");
</s:Group>
                                                      	    	    }
                                                      	    }
                                                      }




                             Topic                             Presenter
                            Designing Testable Code            Bjorn Schultheiss
Business logic and PM’s
Leave me out of it!


  • Presentation Models and Views have a 1 to 1
      relationship

  • Business logic is best separated from PM’s
  • PM’s know about Business/Application classes,
      those classes know nothing about PM’s



                      Topic                     Presenter
                      Designing Testable Code   Bjorn Schultheiss
Well on your way
        Topic
        Designing Testable Code
                                  Presenter
                                  Bjorn Schultheiss
Thanks, questions?




       Topic                     Presenter
       Designing Testable Code   Bjorn Schultheiss

More Related Content

Recently uploaded

Video Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the FutureVideo Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the Future
Alpen-Adria-Universität
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
Jemma Hussein Allen
 
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdfSAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
Peter Spielvogel
 
Elevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object CalisthenicsElevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object Calisthenics
Dorra BARTAGUIZ
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
Adtran
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
OnBoard
 
UiPath Community Day Dubai: AI at Work..
UiPath Community Day Dubai: AI at Work..UiPath Community Day Dubai: AI at Work..
UiPath Community Day Dubai: AI at Work..
UiPathCommunity
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance
 
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
James Anderson
 
The Metaverse and AI: how can decision-makers harness the Metaverse for their...
The Metaverse and AI: how can decision-makers harness the Metaverse for their...The Metaverse and AI: how can decision-makers harness the Metaverse for their...
The Metaverse and AI: how can decision-makers harness the Metaverse for their...
Jen Stirrup
 
Assure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyesAssure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyes
ThousandEyes
 
RESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for studentsRESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for students
KAMESHS29
 
Enhancing Performance with Globus and the Science DMZ
Enhancing Performance with Globus and the Science DMZEnhancing Performance with Globus and the Science DMZ
Enhancing Performance with Globus and the Science DMZ
Globus
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Paige Cruz
 
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptxSecstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
nkrafacyberclub
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance
 
By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024
Pierluigi Pugliese
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance
 
Free Complete Python - A step towards Data Science
Free Complete Python - A step towards Data ScienceFree Complete Python - A step towards Data Science
Free Complete Python - A step towards Data Science
RinaMondal9
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
James Anderson
 

Recently uploaded (20)

Video Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the FutureVideo Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the Future
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
 
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdfSAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
 
Elevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object CalisthenicsElevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object Calisthenics
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
 
UiPath Community Day Dubai: AI at Work..
UiPath Community Day Dubai: AI at Work..UiPath Community Day Dubai: AI at Work..
UiPath Community Day Dubai: AI at Work..
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
 
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
 
The Metaverse and AI: how can decision-makers harness the Metaverse for their...
The Metaverse and AI: how can decision-makers harness the Metaverse for their...The Metaverse and AI: how can decision-makers harness the Metaverse for their...
The Metaverse and AI: how can decision-makers harness the Metaverse for their...
 
Assure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyesAssure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyes
 
RESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for studentsRESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for students
 
Enhancing Performance with Globus and the Science DMZ
Enhancing Performance with Globus and the Science DMZEnhancing Performance with Globus and the Science DMZ
Enhancing Performance with Globus and the Science DMZ
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
 
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptxSecstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
 
By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
 
Free Complete Python - A step towards Data Science
Free Complete Python - A step towards Data ScienceFree Complete Python - A step towards Data Science
Free Complete Python - A step towards Data Science
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
 

Featured

Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
Pixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
marketingartwork
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
Skeleton Technologies
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
SpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Lily Ray
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
Rajiv Jayarajah, MAppComm, ACC
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
Christy Abraham Joy
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
Vit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
MindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
GetSmarter
 

Featured (20)

Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 

Writing testable code

  • 1. Designing Testable Code by Bjorn Schultheiss Topic Presenter Designing Testable Code Bjorn Schultheiss
  • 2. Who am I? Bjorn Schultheiss Developer Adslot Front end developer Flash, Flex and now even some Javascript Large projects including http://www.adchamp.com Topic Presenter Designing Testable Code Bjorn Schultheiss
  • 3. Recent thoughts about Flash Topic Presenter Designing Testable Code Bjorn Schultheiss
  • 4. Find your passion Topic Designing Testable Code Presenter Bjorn Schultheiss
  • 5. Testable code you say This presentation will cover • How to look at OO • Static methods and Global State • Separation of object construction and logic • Constructor Injection vs Setter Injection • Presentation Model Topic Presenter Designing Testable Code Bjorn Schultheiss
  • 6. Sorry I’m busy dude Topic Designing Testable Code Presenter Bjorn Schultheiss
  • 7. And why would I be interested? This presentation is targeted at • Any developer creating heavy client side applications • Focused specifically on Flex but the same principals apply to other web languages and frameworks • you love that feeling from watching a test pass Topic Presenter Designing Testable Code Bjorn Schultheiss
  • 8. Tell me about Flex Unit This talk is not about Flexunit • Flex developer’s best friend • Created by the community • Endorsed by Adobe (integrated in FB) • Easy to get started with Topic Presenter Designing Testable Code Bjorn Schultheiss
  • 9. Topic Presenter Designing Testable Code Bjorn Schultheiss
  • 10. What are unit tests? Topic Presenter Designing Testable Code Bjorn Schultheiss
  • 11. I knew you cowboys were here Topic Designing Testable Code Presenter Bjorn Schultheiss
  • 12. • They are written by software engineers, not test engineers. • They are better written as you code, not after you’ve finished Topic Presenter Designing Testable Code Bjorn Schultheiss
  • 13. The only good excuse not to be writing them is because you don’t know how. Topic Presenter Designing Testable Code Bjorn Schultheiss
  • 14. So what do i need to know? Topic Presenter Designing Testable Code Bjorn Schultheiss
  • 15. How to think about OO • OO is about the relationship between data and code • data represents the state of an object • code modifies that state Topic Presenter Designing Testable Code Bjorn Schultheiss
  • 16. Procedural public class ImageEditor { public function crop(image:Bitmap, size:Rectangle):Bitmap { // do something } Topic Presenter Designing Testable Code Bjorn Schultheiss
  • 17. Better public class ImageEditor { private var _image:Bitmap; public function ImageEditor(image:Bitmap) { _image = image; } public function crop(size:Rectangle):void { // do something } } Topic Presenter Designing Testable Code Bjorn Schultheiss
  • 18. Global State • Object state is subject to garbage collection • Global state is subject to life of the session Topic Presenter Designing Testable Code Bjorn Schultheiss
  • 19. Global State a = new X() --> Topic Presenter Designing Testable Code Bjorn Schultheiss
  • 20. Global State Y a = new X() --> Z X Q Topic Presenter Designing Testable Code Bjorn Schultheiss
  • 21. Global State Y a = new X() --> Z X Q b = new X() --> Topic Presenter Designing Testable Code Bjorn Schultheiss
  • 22. Global State Y a = new X() --> Z X Q Y b = new X() --> Z X Q Topic Presenter Designing Testable Code Bjorn Schultheiss
  • 23. Global State Y a = new X() --> Z a.doSomething(); X Q Y b = new X() --> Z X Q Topic Presenter Designing Testable Code Bjorn Schultheiss
  • 24. Global State Y a = new X() --> Z a.doSomething(); X Q Y b = new X() --> Z b.doSomething(); X Q Topic Presenter Designing Testable Code Bjorn Schultheiss
  • 25. Global State Y a = new X() --> Z a.doSomething(); X Q a == b Y b = new X() --> Z b.doSomething(); X Q Topic Presenter Designing Testable Code Bjorn Schultheiss
  • 26. Global State Y a = new X() --> Z a.doSomething(); X Q GS a == b Y b = new X() --> Z b.doSomething(); X Q Topic Presenter Designing Testable Code Bjorn Schultheiss
  • 27. Global State • Inconsistent results • Order of tests matter • Something the tester doesn’t control Topic Presenter Designing Testable Code Bjorn Schultheiss
  • 28. Singletons Topic Presenter Designing Testable Code Bjorn Schultheiss
  • 29. Singleton’s are pathological liars Topic Designing Testable Code Presenter Bjorn Schultheiss
  • 30. Singletons • Singletons use global state Topic Presenter Designing Testable Code Bjorn Schultheiss
  • 31. package { public class AppSettings { private static const _instance:AppSettings = new AppSettings(Lock); private var _state1:Object; private var _state2:Object; private var _state3:Object; public static function get instance():AppSettings { return _instance; } public function AppSettings(lock:Class) { if (lock!=Lock) throw new Error("cannot unlock"); } } } internal class Lock {} Topic Presenter Designing Testable Code Bjorn Schultheiss
  • 32. Singletons • Singletons use global state • the test doesn’t control the instantion of the process Topic Presenter Designing Testable Code Bjorn Schultheiss
  • 33. package { public class AppSettings { private static const _instance:AppSettings = new AppSettings(Lock); private var _state1:Object; private var _state2:Object; private var _state3:Object; public static function get instance():AppSettings { return _instance; } public function AppSettings(lock:Class) { if (lock!=Lock) throw new Error("cannot unlock"); } } } internal class Lock {} Topic Presenter Designing Testable Code Bjorn Schultheiss
  • 34. Singletons • Singletons use global state • the test doesn’t control the instantion of the process • secret collaborators Topic Presenter Designing Testable Code Bjorn Schultheiss
  • 35. public class CreditCard { private var _gateway:PaymentGateway; public function CreditCard() { gateway = ServiceLocator.getInstance().gateway; } public function chargeCard(value:Number):void { gateway.performTransaction(value); } } var cc:CreditCard = new CreditCard(); cc.chargeCard(10); Topic Presenter Designing Testable Code Bjorn Schultheiss
  • 36. Singletons • Singletons use global state • the test doesn’t control the instantion of the process • secret collaborators • you may know the dependencies, but anyone that comes after you is baffled! Topic Presenter Designing Testable Code Bjorn Schultheiss
  • 37. Law of Demeter Object.getSomething.theProperty Topic Presenter Designing Testable Code Bjorn Schultheiss
  • 38. Law of Demeter Object.getSomething.theProperty Topic Presenter Designing Testable Code Bjorn Schultheiss
  • 39. Law of Demeter Object.getSomething.theProperty something.theProperty Topic Presenter Designing Testable Code Bjorn Schultheiss
  • 40. Locator in action class House { public function House(locator:Locator) { // what needs to be mocked in test? } } Topic Presenter Designing Testable Code Bjorn Schultheiss
  • 41. Use revealed class House { private var _door:Door; private var _roof:Roof; private var _window:Window public function House(locator:Locator) { API gave us _door = locator.getDoor(); _roof = locator.getRoof(); no help _window = locator.getWindow(); } } Topic Presenter Designing Testable Code Bjorn Schultheiss
  • 42. An API that helps class House { private var _door:Door; private var _roof:Roof; private var _window:Window public function House(door:Door, roof:Roof, window:Window) { _door = door; _roof = roof; _window = window; } } Topic Presenter Designing Testable Code Bjorn Schultheiss
  • 43. Similar Anti-Patterns I didn’t ask for that!! • aka Locator • aka Context • aka Manager • Hides true dependencies • Breaks law of demeter Topic Presenter Designing Testable Code Bjorn Schultheiss
  • 44. Constructor vs Setter injection • An order of instantiation Topic Presenter Designing Testable Code Bjorn Schultheiss
  • 45. Setter example <s:Group xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark"> <fx:Script> <![CDATA[ import mx.collections.ArrayCollection; import mx.collections.IList; import mx.rpc.AsyncToken; public var userService:Service; public function findUsers():void { userService.findAll(); userService.addEventListener("onResult", onResult); } public function onResult(result:Object):void { users.dataprovider = new ArrayCollection(result); } ]]> </fx:Script> <s:Button label="find all users" click="findUsers();" /> <s:List id="users" /> </s:Group> Topic Presenter Designing Testable Code Bjorn Schultheiss
  • 46. Constructor example public class ShowUserModel { [Bindable] public var users:IList; private var _service:Service; public function ShowUserModel(service:Service) { _service = service; _service.addEventListener("onResult", onResult); } public function findUsers():void { _service.findAll(); } private function onResult(result:Object):void { users = new ArrayCollection(result); } } Topic Presenter Designing Testable Code Bjorn Schultheiss
  • 47. Instantiation example [Test] public function testShowUsersComponent():void { var service:Service = new Service("Users"); var component:ShowUsersComponent = new ShowUsersComponent(); component.service = service; component.findUsers(); } [Test] public function testShowUsersModel():void { var service:Service = new Service("Users"); var model:ShowUsersModel = new ShowUsersModel(service); model.findUsers(); } Topic Presenter Designing Testable Code Bjorn Schultheiss
  • 48. How does this relate to unit testing? Topic Presenter Designing Testable Code Bjorn Schultheiss
  • 49. How does this relate to unit testing? Unit testing as the name implies you test a Class (unit) in isolation Topic Presenter Designing Testable Code Bjorn Schultheiss
  • 50. If your class mixes Object construction and logic you will never be able to achieve isolation Topic Presenter Designing Testable Code Bjorn Schultheiss
  • 51. In order to unit test you need to separate your object graph construction from the logic into 2 different classes Topic Presenter Designing Testable Code Bjorn Schultheiss
  • 52. The end goal is to have classes with either logic or “new” operators Topic Presenter Designing Testable Code Bjorn Schultheiss
  • 53. Construction and logic mixed public class House { private var _kitchen:Kitchen = new Kitchen(); private var _isLocked:Boolean; public function get isLocked():Boolean { return _isLocked; } public function lock() { _kitchen.lock(); _isLocked = true; } } Topic Presenter Designing Testable Code Bjorn Schultheiss
  • 54. Injection example public class House { private var _kitchen:Kitchen; private var _isLocked:Boolean; public function House(kitchen:Kitchen) { _kitchen = kitchen; } public function get isLocked():Boolean { return _isLocked; } public function lock() { _kitchen.lock(); _isLocked = true; } } Topic Presenter Designing Testable Code Bjorn Schultheiss
  • 55. Construction example function build():House { return new House(new Kitchen( new Sink(), new Dishwasher(), new Refrigerator()) ); } Topic Presenter Designing Testable Code Bjorn Schultheiss
  • 56. lets look at writing a unit test Topic Presenter Designing Testable Code Bjorn Schultheiss
  • 57. Writing a unit test to test a method first you need to instantiate an object • Specifically the class or component to be tested • Any collaborators (real or mock). Topic Presenter Designing Testable Code Bjorn Schultheiss
  • 58. [Test] public function testHouse():void { var kitchen:Kitchen = new Kitchen(); var house:House = new House(kitchen); house.testMethod(); } Topic Presenter Designing Testable Code Bjorn Schultheiss
  • 59. Testing UI Components Topic Presenter Designing Testable Code Bjorn Schultheiss
  • 60. Spare me Topic Designing Testable Code Presenter Bjorn Schultheiss
  • 61. Testing UI Components Possible but generally a pain, why? • test a style is applied • mimic user interaction • logic is is mixed with view Topic Presenter Designing Testable Code Bjorn Schultheiss
  • 62. [Before(async,ui)] public function setUp() : void { myButton = new MyButton(); Async.proceedOnEvent( this, myButton, FlexEvent.CREATION_COMPLETE, 100 ); UIImpersonator.addChild( myButton ); }     [Test(async,ui)] public function myButton_myStyle_defaultValue() : void { Assert.assertEquals( myButton.getStyle( "myStyle" ), "expectedDefaultValue" ); } Topic Presenter Designing Testable Code Bjorn Schultheiss
  • 63. UI Codefendants • UIImpersonator • invalidationModel • [Async] proceedOnEvent() • displayList hierarchy Topic Presenter Designing Testable Code Bjorn Schultheiss
  • 64. Presentation Models http://martinfowler.com/eaaDev/PresentationModel.html Represent the state and behaviour of the presentation independently of the GUI controls used in the interface • uses Binding to interact with view • also handles events from view • a pattern used in popular frameworks • great for unit testing Topic Presenter Designing Testable Code Bjorn Schultheiss
  • 65. Presentation Model example <s:Group xmlns:fx="http://ns.adobe.com/mxml/2009"> package presentation <fx:Script> { <![CDATA[ [Bindable] import presentation.HelloWorldModel; public class HelloWorldModel [Bindable] { public var model:HelloWorldModel; public var label:String = "Hello"; ]]> </fx:Script> public function clickHandler():void <s:Label text="{ model.label }" { click="model.clickHandler();" /> trace("Is it me your looking for?"); </s:Group> } } } Topic Presenter Designing Testable Code Bjorn Schultheiss
  • 66. Business logic and PM’s Leave me out of it! • Presentation Models and Views have a 1 to 1 relationship • Business logic is best separated from PM’s • PM’s know about Business/Application classes, those classes know nothing about PM’s Topic Presenter Designing Testable Code Bjorn Schultheiss
  • 67. Well on your way Topic Designing Testable Code Presenter Bjorn Schultheiss
  • 68. Thanks, questions? Topic Presenter Designing Testable Code Bjorn Schultheiss

Editor's Notes

  1. I want to talk about future development opportunities for the adbuilder,\nmore premium engaging forms of advertising, and how to use html as an\noption for ad deployment.\n
  2. \n
  3. \n
  4. \n
  5. \n
  6. \n
  7. \n
  8. \n
  9. \n
  10. \n
  11. \n
  12. \n
  13. \n
  14. \n
  15. \n
  16. - doesnt interact with class data\n- users passed in to method\n
  17. \n
  18. \n
  19. \n
  20. \n
  21. \n
  22. \n
  23. \n
  24. \n
  25. \n
  26. \n
  27. \n
  28. \n
  29. \n
  30. \n
  31. \n
  32. \n
  33. \n
  34. \n
  35. \n
  36. \n
  37. \n
  38. \n
  39. \n
  40. \n
  41. \n
  42. \n
  43. \n
  44. \n
  45. \n
  46. \n
  47. \n
  48. \n
  49. \n
  50. \n
  51. \n
  52. \n
  53. \n
  54. \n
  55. \n
  56. \n
  57. \n
  58. \n
  59. \n
  60. \n
  61. \n
  62. \n
  63. \n
  64. \n
  65. \n
  66. \n
  67. \n
  68. \n