Beyond Basic Unit Testing Mocks Stubs User Interfaces
Beyond Basic Unit Testing: Mocks, Stubs, User Interfaces, and Refactoring for Testability Benjamin Day http: //benday. com
Who am I? l Owner, Benjamin Day Consulting, Inc. l Trainer, Consultant l Microsoft MVP for VSTS Microsoft VSTS/TFS Customer Advisory Council Microsoft Cloud Services Advisory Group Leader of Beantown. NET INETA User Group l l l – Email: benday@benday. com – Web: http: //www. benday. com – Blog: http: //blog. benday. com – Visual Studio Team System, Team Foundation Server
Overview l Why do you care about unit testing? l Testing Problems l Testing Solutions Code l
WHAT & WHY?
What is Test Driven Development? l Way of developing code so that you always have proof that something is working – Code that validates other code – Small chunks of “is it working? ” l l Small chunks = Unit Tests Kent Beck (“Test-Driven Development”, Addison-Wesley) says “Never write a single line of code unless you have a failing automated test. ”
What is a Unit Test? l Wikipedia says, “a unit test is a procedure used to validate that a particular module of source code is working properly” l Method that exercises another piece of code and checks results and object state using assertions
Why Use TDD? l High-quality code – Fewer bugs – Bugs are easier to diagnose l l l “Test First” method ~up-front design Less time spent in the debugger Tests that say when something works – Easier maintenance easier refactoring – Self documenting
Maximize Your QA Staff l You shouldn’t need QA to tell your code doesn’t work l Unit testing to minimizes the pointless bugs – “nothing happened” – “I got an error message” + stack trace – Null. Reference. Exception l QA should be checking for: – Meets requirements – Usability problems – Visual things (colors, fonts, etc) l When you get a bug assigned to you it should add business value
PROBLEMS & SOLUTIONS
Testing Problems l Extra code just for the sake of the test l Code coverage on exception handling l Tendency toward end-to-end integration tests – Databases – WCF & Services – Big back-end systems l Unit testing UIs
Testing Solutions l Mocks, Stubs, and Mocking Frameworks l Interface-driven coding l Factory Pattern and/or Io. C Frameworks Repository Pattern Model-View-Presenter (MVP) Pattern Model-View-Controller (MVC) Pattern l l l
Mocks vs Stubs vs Dummies vs Fakes l Martin Fowler http: //martinfowler. com/articles/ mocks. Arent. Stubs. html l Dummy = passed but not used Fake = “shortcut” implementation Stub = Only pretends to work, returns predefined answer Mock = Used to test expectations, requires verification at the end of test l l l
Rhino. Mocks l Dynamic Mocking Framework l By Ayende Rahien http: //ayende. com/projects/rhino-mocks. aspx Free under the BSD license l
Rhino. Mocks Primer l Mock. Repository – Owns the mocking session l l l Strict. Mock<T>() Call order sensitive Dynamic. Mock<T>() Ignores call order Stub<T>() – Ignores Order – Create get/set properties automatically l Replay. All() – Marks start of the testing
Demo 1: Stub With Rhino. Mocks
Demo 2: Test Exception Handling l Look at some existing code l Refactor for testability l Use Rhino. Mocks to trigger the exception handler
Avoid End-to-End Integration Tests Does a good test… l l l …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?
The Repository Pattern l “Mediates between the domain and data mapping layers using a collection-like interface for accessing domain objects. ” – http: //martinfowler. com/eaa. Catalog/repository. html l Encapsulates the logic of getting things saved and retrieved
Person Repository
Demo 3: Repository Pattern l Simplify database (or web service) unit test with a repository
USER INTERFACE TESTING
User Interfaces: The Redheaded Stepchild of the Unit Testing World l Not easy to automate the UI testing l Basically, automating button clicks l 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 l The rest is: – Exercising the application – Checking that fields have the right data – Checking field visibility
VSTS Web Tests l Record paths through your application l Fills in form values l l Click buttons Validates l Difficult to do test-driven development
My $0. 02 l Solve the problem by not solving the problem l Find a way to minimize what you can’t automate
The Solution. l 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 l l l Test the business tier “Transaction Script” Pattern “Domain Model” Pattern “Service Layer” Pattern “Model View Presenter” Pattern “Model View Controller” Pattern
Service Layer Pattern “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 From “Patterns Of Enterprise Application Architecture” by Martin Fowler, Randy Stafford, et al. Chapter 9
Model View Presenter (MVP)
Model View Presenter (MVP)
The Common Tiers l Presentation tier – – – l l – Business object interfaces – Business objects ASP. NET Windows Forms WPF • The “Model” in MVP – Business facades WCF Service The “View” of MVP • Manipulate business objects • Handle requests for CRUD operations Presenter Tier – Handles the "conversation" between the presentation tier implementation and the business tier – Defines the “View” Interfaces – “Presenter” in MVP Business tier l Data Access Tier l Data Storage Tier – SQL Server
Tiering Up: Keep Logic Out Of The UIs l Business Object Tier (Domain Model pattern) l Business Façade Tier (Service Layer pattern) – Create new Business Object methods (Factory methods) – Wrap CRUD operations, abstract away data access logic – Duplicate name checks l l Create an interface to represent each page in your application Create Editor Facades as an adapter between the UI interfaces and the business objects
View interfaces l Interface represents the fields manipulated through the UI l ASPX Page or Windows Form Implements the interface – Interface’s properties wrap UI widgets – ICustomer. Detail. Customer. Name m_textbox. Customer. Name. Text l l l Use a stub represent the UI Write unit tests to test the functionality of the presenter Avoid business objects favor scalars
The Presenter l Service Layer Pattern l Wrap larger operations that are relevant to each UI page/screen interface – Initialize. Blank(ICustomer. Detail) – View(ICustomer. Detail) – Save(ICustomer. Detail) l Since each page implements the interface, pass the page reference to the facade
Model View Presenter (MVP)
Designing the UI for Testability Person. Detail. View. aspx
Why is this more testable? l Each page/screen only has to get/set the value from interface property into the right display control l UI does not know anything about business objects Doesn’t know about any details of loading or saving l l Doesn’t have to know about validation l All this logic goes into the editor façade and testable via unit test
Avoid Referencing Business Objects in the UI “interfaces” l ASP. NET – Easier to write stateless pages (everything is in View. State) – No need to try to re-create complex objects in code behind in order to save
Demo 4 l Refactor to UI Interfaces
Summary: Testing Solutions l Mocks, Stubs, and Mocking Frameworks l Interface-driven coding l Factory Pattern and/or Io. C Frameworks Repository Pattern Model-View-Presenter (MVP) Pattern l l
Who am I? l Owner, Benjamin Day Consulting, Inc. l Trainer, Consultant l Microsoft MVP for VSTS Microsoft VSTS/TFS Customer Advisory Council Microsoft Cloud Services Advisory Group Leader of Beantown. NET INETA User Group l l l – Email: benday@benday. com – Web: http: //www. benday. com – Blog: http: //blog. benday. com – Visual Studio Team System, Team Foundation Server
- Slides: 39