design for testability: mocks, stubs, refactoring, and user interfaces benjamin day benday.com |...
Post on 19-Dec-2015
225 views
TRANSCRIPT
![Page 1: Design for Testability: Mocks, Stubs, Refactoring, and User Interfaces Benjamin Day benday.com | blog.benday.com @benday](https://reader036.vdocuments.mx/reader036/viewer/2022081515/56649d275503460f949fde7e/html5/thumbnails/1.jpg)
Design for Testability:Mocks, Stubs, Refactoring, and User
Interfaces
Design for Testability:Mocks, Stubs, Refactoring, and User
InterfacesBenjamin DayBenjamin Day
benday.com | blog.benday.com@benday
![Page 2: Design for Testability: Mocks, Stubs, Refactoring, and User Interfaces Benjamin Day benday.com | blog.benday.com @benday](https://reader036.vdocuments.mx/reader036/viewer/2022081515/56649d275503460f949fde7e/html5/thumbnails/2.jpg)
Introductions: Benjamin DayIntroductions: Benjamin Day• Consultant, Coach, Trainer• Scrum.org Classes
• Professional Scrum Developer (PSD)
• Professional Scrum Foundations (PSF)
• TechEd, VSLive, DevTeach, O’Reilly OSCON• Visual Studio Magazine, Redmond Developer News• Microsoft MVP for Visual Studio ALM• Team Foundation Server, TDD, Testing Best Practices,
Silverlight, Windows Azure• http://blog.benday.com• [email protected]
![Page 3: Design for Testability: Mocks, Stubs, Refactoring, and User Interfaces Benjamin Day benday.com | blog.benday.com @benday](https://reader036.vdocuments.mx/reader036/viewer/2022081515/56649d275503460f949fde7e/html5/thumbnails/3.jpg)
AgendaAgenda
• Quick ‘what’ and ‘why’• Dependency Injection • Mocks & Stubs• Databases, Web Services• User Interface Testing
![Page 4: Design for Testability: Mocks, Stubs, Refactoring, and User Interfaces Benjamin Day benday.com | blog.benday.com @benday](https://reader036.vdocuments.mx/reader036/viewer/2022081515/56649d275503460f949fde7e/html5/thumbnails/4.jpg)
WHAT & WHYWHAT & WHY
![Page 5: Design for Testability: Mocks, Stubs, Refactoring, and User Interfaces Benjamin Day benday.com | blog.benday.com @benday](https://reader036.vdocuments.mx/reader036/viewer/2022081515/56649d275503460f949fde7e/html5/thumbnails/5.jpg)
What is Test Driven Development?What is Test Driven Development?
• Develop code with proof that it works• Code that validates other code
• Small chunks of “is it working?”
• Small chunks = Unit Tests• “Never write a single line of code unless
you have a failing automated test.”• Kent Beck, “Test-Driven Development”,
Addison-Wesley
![Page 6: Design for Testability: Mocks, Stubs, Refactoring, and User Interfaces Benjamin Day benday.com | blog.benday.com @benday](https://reader036.vdocuments.mx/reader036/viewer/2022081515/56649d275503460f949fde7e/html5/thumbnails/6.jpg)
Why Use TDD?Why Use TDD?
• High-quality code • Fewer bugs• Bugs are easier to diagnose
• Encourages you to think about…• …what you’re building• …how you know you’re done• …how you know it works
• Less time in the debugger• Tests that say when something works
• Easier maintenance, refactoring• Self-documenting
![Page 7: Design for Testability: Mocks, Stubs, Refactoring, and User Interfaces Benjamin Day benday.com | blog.benday.com @benday](https://reader036.vdocuments.mx/reader036/viewer/2022081515/56649d275503460f949fde7e/html5/thumbnails/7.jpg)
Maximize Your QA StaffMaximize Your QA Staff
• You shouldn’t need QA to tell you your code doesn’t work
• Unit tests minimize the pointless bugs• “nothing happened”
• “I got an error message” + stack trace
• NullReferenceException
• QA should be checking for:• Stuff only a human can test
• User Story / Product Backlog Item
• Bug assigned to you should add business value
![Page 8: Design for Testability: Mocks, Stubs, Refactoring, and User Interfaces Benjamin Day benday.com | blog.benday.com @benday](https://reader036.vdocuments.mx/reader036/viewer/2022081515/56649d275503460f949fde7e/html5/thumbnails/8.jpg)
How would you test this?How would you test this?
![Page 9: Design for Testability: Mocks, Stubs, Refactoring, and User Interfaces Benjamin Day benday.com | blog.benday.com @benday](https://reader036.vdocuments.mx/reader036/viewer/2022081515/56649d275503460f949fde7e/html5/thumbnails/9.jpg)
What is Design For Testability?What is Design For Testability?
• Build it so you can test it.
How would you test this?
Do you have to take the plane up for a spin?
http://www.flickr.com/photos/ericejohnson/4427453880/
![Page 10: Design for Testability: Mocks, Stubs, Refactoring, and User Interfaces Benjamin Day benday.com | blog.benday.com @benday](https://reader036.vdocuments.mx/reader036/viewer/2022081515/56649d275503460f949fde7e/html5/thumbnails/10.jpg)
DEPENDENCY INJECTIONDEPENDENCY INJECTION
![Page 11: Design for Testability: Mocks, Stubs, Refactoring, and User Interfaces Benjamin Day benday.com | blog.benday.com @benday](https://reader036.vdocuments.mx/reader036/viewer/2022081515/56649d275503460f949fde7e/html5/thumbnails/11.jpg)
What is Dependency Injection?What is Dependency Injection?
• Helps you to design for testability
• Classes advertise their dependencies on the constructor• Need to save & load Person objects?
Add a person data access instance on the constructor.
• Related to the Factory Pattern• Keeps classes loosely coupled
![Page 12: Design for Testability: Mocks, Stubs, Refactoring, and User Interfaces Benjamin Day benday.com | blog.benday.com @benday](https://reader036.vdocuments.mx/reader036/viewer/2022081515/56649d275503460f949fde7e/html5/thumbnails/12.jpg)
Try a DI frameworkTry a DI framework
• Can make life easier• I use the Unity Framework from MSFT
Patterns & Practices.• There are half-zillion frameworks out
there. They’re pretty much all the same.• Castle, Structure Map, etc.
• TIP: hide the existence of your DI framework from the rest of your application.
![Page 13: Design for Testability: Mocks, Stubs, Refactoring, and User Interfaces Benjamin Day benday.com | blog.benday.com @benday](https://reader036.vdocuments.mx/reader036/viewer/2022081515/56649d275503460f949fde7e/html5/thumbnails/13.jpg)
Why does DI help with testability?Why does DI help with testability?
• Helps you focus on the testing task at hand• Only test what you’re trying to test. Skip everything
else.
• Makes interface-driven programming simple
• Interface-driven programming + DI lets you use mocks and stubs in your tests.
![Page 14: Design for Testability: Mocks, Stubs, Refactoring, and User Interfaces Benjamin Day benday.com | blog.benday.com @benday](https://reader036.vdocuments.mx/reader036/viewer/2022081515/56649d275503460f949fde7e/html5/thumbnails/14.jpg)
MOCKS & STUBSMOCKS & STUBS
![Page 15: Design for Testability: Mocks, Stubs, Refactoring, and User Interfaces Benjamin Day benday.com | blog.benday.com @benday](https://reader036.vdocuments.mx/reader036/viewer/2022081515/56649d275503460f949fde7e/html5/thumbnails/15.jpg)
Mocks vs. Stubs vs. Mocks vs. Stubs vs. Dummies vs. FakesDummies vs. Fakes
• Martin Fowlerhttp://martinfowler.com/articles/mocksArentStubs.html
• Dummy = passed but not used• Fake = “shortcut” implementation• Stub = Only pretends to work, returns pre-
defined answer• Mock = Used to test expectations, requires
verification at the end of test
![Page 16: Design for Testability: Mocks, Stubs, Refactoring, and User Interfaces Benjamin Day benday.com | blog.benday.com @benday](https://reader036.vdocuments.mx/reader036/viewer/2022081515/56649d275503460f949fde7e/html5/thumbnails/16.jpg)
RhinoMocksRhinoMocks
• Dynamic Mocking Framework• By Ayende Rahien
http://ayende.com/projects/rhino-mocks.aspx
• Free under the BSD license
![Page 17: Design for Testability: Mocks, Stubs, Refactoring, and User Interfaces Benjamin Day benday.com | blog.benday.com @benday](https://reader036.vdocuments.mx/reader036/viewer/2022081515/56649d275503460f949fde7e/html5/thumbnails/17.jpg)
RhinoMocks PrimerRhinoMocks Primer
• MockRepository• Owns the mocking session
• StrictMock<T>() Call order sensitive• DynamicMock<T>() Ignores call order• Stub<T>()
• Ignores Order
• Create get/set properties automatically
• ReplayAll()• Marks start of the testing
![Page 18: Design for Testability: Mocks, Stubs, Refactoring, and User Interfaces Benjamin Day benday.com | blog.benday.com @benday](https://reader036.vdocuments.mx/reader036/viewer/2022081515/56649d275503460f949fde7e/html5/thumbnails/18.jpg)
Demo 1: Stub With RhinoMocksDemo 1: Stub With RhinoMocks
![Page 19: Design for Testability: Mocks, Stubs, Refactoring, and User Interfaces Benjamin Day benday.com | blog.benday.com @benday](https://reader036.vdocuments.mx/reader036/viewer/2022081515/56649d275503460f949fde7e/html5/thumbnails/19.jpg)
Demo 2: Test Exception HandlingDemo 2: Test Exception Handling
• Look at some existing code• Refactor for testability• Use RhinoMocks to trigger the exception
handler
![Page 20: Design for Testability: Mocks, Stubs, Refactoring, and User Interfaces Benjamin Day benday.com | blog.benday.com @benday](https://reader036.vdocuments.mx/reader036/viewer/2022081515/56649d275503460f949fde7e/html5/thumbnails/20.jpg)
DATABASES & SERVICESDATABASES & SERVICES
![Page 21: Design for Testability: Mocks, Stubs, Refactoring, and User Interfaces Benjamin Day benday.com | blog.benday.com @benday](https://reader036.vdocuments.mx/reader036/viewer/2022081515/56649d275503460f949fde7e/html5/thumbnails/21.jpg)
Avoid End-to-End Integration TestsAvoid End-to-End Integration Tests
Does a good test…• …really have to write all the way to the
database?• …really have to have a running WCF
service on the other end of that call?• …really need to make a call to the
mainframe?
![Page 22: Design for Testability: Mocks, Stubs, Refactoring, and User Interfaces Benjamin Day benday.com | blog.benday.com @benday](https://reader036.vdocuments.mx/reader036/viewer/2022081515/56649d275503460f949fde7e/html5/thumbnails/22.jpg)
Reminder: Reminder: Only test what you have to test.Only test what you have to test.
To test this latch, do you have to take the plane up for a spin?
http://www.flickr.com/photos/ericejohnson/4427453880/
![Page 23: Design for Testability: Mocks, Stubs, Refactoring, and User Interfaces Benjamin Day benday.com | blog.benday.com @benday](https://reader036.vdocuments.mx/reader036/viewer/2022081515/56649d275503460f949fde7e/html5/thumbnails/23.jpg)
The Repository PatternThe Repository Pattern
• “Mediates between the domain and data mapping layers using a collection-like interface for accessing domain objects.”• http://martinfowler.com/eaaCatalog/repository.html
• Encapsulates the logic of getting things saved and retrieved
![Page 24: Design for Testability: Mocks, Stubs, Refactoring, and User Interfaces Benjamin Day benday.com | blog.benday.com @benday](https://reader036.vdocuments.mx/reader036/viewer/2022081515/56649d275503460f949fde7e/html5/thumbnails/24.jpg)
Person RepositoryPerson Repository
![Page 25: Design for Testability: Mocks, Stubs, Refactoring, and User Interfaces Benjamin Day benday.com | blog.benday.com @benday](https://reader036.vdocuments.mx/reader036/viewer/2022081515/56649d275503460f949fde7e/html5/thumbnails/25.jpg)
Demo 3: Repository PatternDemo 3: Repository Pattern
• Simplify database (or web service) unit test with a repository
![Page 26: Design for Testability: Mocks, Stubs, Refactoring, and User Interfaces Benjamin Day benday.com | blog.benday.com @benday](https://reader036.vdocuments.mx/reader036/viewer/2022081515/56649d275503460f949fde7e/html5/thumbnails/26.jpg)
USER INTERFACESUSER INTERFACES
![Page 27: Design for Testability: Mocks, Stubs, Refactoring, and User Interfaces Benjamin Day benday.com | blog.benday.com @benday](https://reader036.vdocuments.mx/reader036/viewer/2022081515/56649d275503460f949fde7e/html5/thumbnails/27.jpg)
User Interfaces: The Redheaded User Interfaces: The Redheaded Stepchild of the Unit Testing WorldStepchild of the Unit Testing World• Not easy to automate the UI testing• Basically, automating button clicks• UI’s almost have to be tested by a human
• Computers don’t understand the “visual stuff”
• Colors, fonts, etc are hard to unit test for
• “This doesn’t look right” errors
• The rest is:• Exercising the application
• Checking that fields have the right data
• Checking field visibility
![Page 28: Design for Testability: Mocks, Stubs, Refactoring, and User Interfaces Benjamin Day benday.com | blog.benday.com @benday](https://reader036.vdocuments.mx/reader036/viewer/2022081515/56649d275503460f949fde7e/html5/thumbnails/28.jpg)
My $0.02My $0.02
• Solve the problem by not solving the problem
• Find a way to minimize what you can’t automate
![Page 29: Design for Testability: Mocks, Stubs, Refactoring, and User Interfaces Benjamin Day benday.com | blog.benday.com @benday](https://reader036.vdocuments.mx/reader036/viewer/2022081515/56649d275503460f949fde7e/html5/thumbnails/29.jpg)
The Solution.The Solution.
• Keep as much logic as possible out of the UI• Shouldn’t be more than a handful of assignments
• Nothing smart
• Real work is handled by the business tier
• Test the UI separate from everything else
![Page 30: Design for Testability: Mocks, Stubs, Refactoring, and User Interfaces Benjamin Day benday.com | blog.benday.com @benday](https://reader036.vdocuments.mx/reader036/viewer/2022081515/56649d275503460f949fde7e/html5/thumbnails/30.jpg)
Design Patterns for UI TestabilityDesign Patterns for UI Testability
• Model-View-Controller (MVC)• ASP.NET MVC
• Model-View-Presenter (MVP)• Windows Forms
• ASP.NET Web Forms
• Model-View-ViewModel (MVVM)• Silverlight
• WPF
• Windows Phone 7
![Page 31: Design for Testability: Mocks, Stubs, Refactoring, and User Interfaces Benjamin Day benday.com | blog.benday.com @benday](https://reader036.vdocuments.mx/reader036/viewer/2022081515/56649d275503460f949fde7e/html5/thumbnails/31.jpg)
Service Layer PatternService Layer Pattern
From “Patterns Of Enterprise Application Architecture”by Martin Fowler, Randy Stafford, et al.Chapter 9
“Defines an application’s boundary with a layer of services that establishes a set of available operations and coordinates the application’s response in each operation.”
-Randy Stafford
![Page 32: Design for Testability: Mocks, Stubs, Refactoring, and User Interfaces Benjamin Day benday.com | blog.benday.com @benday](https://reader036.vdocuments.mx/reader036/viewer/2022081515/56649d275503460f949fde7e/html5/thumbnails/32.jpg)
Model View Presenter (MVP)Model View Presenter (MVP)
![Page 33: Design for Testability: Mocks, Stubs, Refactoring, and User Interfaces Benjamin Day benday.com | blog.benday.com @benday](https://reader036.vdocuments.mx/reader036/viewer/2022081515/56649d275503460f949fde7e/html5/thumbnails/33.jpg)
Model View Presenter (MVP)Model View Presenter (MVP)
![Page 34: Design for Testability: Mocks, Stubs, Refactoring, and User Interfaces Benjamin Day benday.com | blog.benday.com @benday](https://reader036.vdocuments.mx/reader036/viewer/2022081515/56649d275503460f949fde7e/html5/thumbnails/34.jpg)
The Common TiersThe Common Tiers
• Presentation tier• ASP.NET
• Windows Forms
• WPF
• WCF Service
• The “View” of MVP
• Presenter Tier• Handles the "conversation"
between the presentation tier implementation and the business tier
• Defines the “View” Interfaces
• “Presenter” in MVP
• Business tier• Business object
interfaces
• Business objects•The “Model” in MVP
• Business facades •Manipulate business objects•Handle requests for CRUD operations
• Data Access Tier• Data Storage Tier
• SQL Server
![Page 35: Design for Testability: Mocks, Stubs, Refactoring, and User Interfaces Benjamin Day benday.com | blog.benday.com @benday](https://reader036.vdocuments.mx/reader036/viewer/2022081515/56649d275503460f949fde7e/html5/thumbnails/35.jpg)
View interfacesView interfaces
• Interface represents the fields manipulated through the UI
• ASPX Page or Windows Form Implements the interface• Interface’s properties wrap UI widgets
• ICustomerDetailView.CustomerName m_textboxCustomerName.Text
• Use a stub represent the UI• Write unit tests to test the functionality of
the presenter• Avoid business objects favor scalars
![Page 36: Design for Testability: Mocks, Stubs, Refactoring, and User Interfaces Benjamin Day benday.com | blog.benday.com @benday](https://reader036.vdocuments.mx/reader036/viewer/2022081515/56649d275503460f949fde7e/html5/thumbnails/36.jpg)
The PresenterThe Presenter
• Service Layer Pattern• Wrap larger operations that are relevant to
each UI page/screen interface• InitializeBlank(ICustomerDetailView)
• View(ICustomerDetailView)
• Save(ICustomerDetailView)
• Since each page implements the interface, pass the page reference to the facade
![Page 37: Design for Testability: Mocks, Stubs, Refactoring, and User Interfaces Benjamin Day benday.com | blog.benday.com @benday](https://reader036.vdocuments.mx/reader036/viewer/2022081515/56649d275503460f949fde7e/html5/thumbnails/37.jpg)
Model View Presenter (MVP)Model View Presenter (MVP)
![Page 38: Design for Testability: Mocks, Stubs, Refactoring, and User Interfaces Benjamin Day benday.com | blog.benday.com @benday](https://reader036.vdocuments.mx/reader036/viewer/2022081515/56649d275503460f949fde7e/html5/thumbnails/38.jpg)
Designing the UI for TestabilityDesigning the UI for TestabilityPersonDetailView.aspx
![Page 39: Design for Testability: Mocks, Stubs, Refactoring, and User Interfaces Benjamin Day benday.com | blog.benday.com @benday](https://reader036.vdocuments.mx/reader036/viewer/2022081515/56649d275503460f949fde7e/html5/thumbnails/39.jpg)
Why is this more testable?Why is this more testable?
• Each page/screen only has to get/set the value from interface property into the right display control
• UI does not know anything about business objects
• Doesn’t know about any details of loading or saving
• Doesn’t have to know about validation
• All this logic goes into the Presenter and testable via unit test
![Page 40: Design for Testability: Mocks, Stubs, Refactoring, and User Interfaces Benjamin Day benday.com | blog.benday.com @benday](https://reader036.vdocuments.mx/reader036/viewer/2022081515/56649d275503460f949fde7e/html5/thumbnails/40.jpg)
Demo 4Demo 4
• Refactor to UI Interfaces
![Page 41: Design for Testability: Mocks, Stubs, Refactoring, and User Interfaces Benjamin Day benday.com | blog.benday.com @benday](https://reader036.vdocuments.mx/reader036/viewer/2022081515/56649d275503460f949fde7e/html5/thumbnails/41.jpg)
AgendaAgenda
• Quick ‘what’ and ‘why’• Dependency Injection • Mocks & Stubs• Databases, Web Services• User Interface Testing