Design for Testability: Mocks, Stubs, Refactoring, and User Interfaces Benjamin Day benday.com |

Slides:



Advertisements
Similar presentations
Software Testing with Visual Studio 2013 & Team Foundation Server 2013 Benjamin Day.
Advertisements

Test-Driven Development José Almeida, Microsoft
A Brief Introduction to Test- Driven Development Shawn M. Jones.
Real World Scrum with TFS2013 Benjamin Day. Brookline, MA Consultant, Coach, & Trainer Microsoft MVP for Visual Studio ALM Team Foundation Server, Software.
Spring, Hibernate and Web Services 13 th September 2014.
How to be a C# ninja in 10 easy steps. Benjamin Day.
Real World Scrum with Team Foundation Server 2013 Benjamin
Team Foundation Server 2010 Builds: Understand, Configure, and Customize Benjamin Day benday.com |
(c) 2002 Object Mentor, Inc.1 Testing Things That Seem Hard To Test Robert Koss, Ph. D. ObjectMentor, Inc.
Design and Planning Presenter: Mike Team Members: Casey Kaushik Danny.
Design Patterns for MVVM Unit Testing & Testability Benjamin Day.
Real world Windows Phone development Igor
Tutorial -01. Objective In this session we will discuss about : 1.What is MVC? 2.Why MVC? 3.Advantages of MVC over ASP.NET 4.ASP.NET development models.
By Bob Bunson  Simulation of software development project  Fictitious system from Concept to Code  Oriented around the.
Microsoft ® Official Course Monitoring and Troubleshooting Custom SharePoint Solutions SharePoint Practice Microsoft SharePoint 2013.
UNIT-V The MVC architecture and Struts Framework.
Test-Driven Development With Visual Studio 2005 Erno de Weerd Info Support.
Design for Testability: Mocks, Stubs, Refactoring, and User Interfaces Benjamin Day.
10 Ways to Get Your Project Started Right Benjamin Day.
Entity Framework Code First End to End
Presenter - Donn Felker.  Senior Consultant for Microsoft Gold Certified Partner- Statêra.  8 years of experience in developing and architecting enterprise.
@benday #vslive Better Unit Tests through Design Patterns: Repository, Adapter, Mocks, and more… Benjamin
Doing Something Useful with Enterprise Library 3.0 Benjamin Day Level: Intermediate.
NOTE: To change the image on this slide, select the picture and delete it. Then click the Pictures icon in the placeholder to insert your own image. WEB.
@benday #vslive Automated Build, Test & Deploy with TFS, ASP.NET, and SQL Server Benjamin
MVC and MVP. References enter.html enter.html
Todd Snyder Development Team Lead Infragistics Experience Design Group.
Windows Azure Tour Benjamin Day Benjamin Day Consulting, Inc.
Building an Offline Smart Client using Domain-Driven Design Principles Tim McCarthy.
Top 7 Lessons From My First Big Silverlight Project Benjamin Day Benjamin Day Consulting, Inc.
Top 10 Ways to Go from Good to Great Scrum Master Benjamin Day.
Team Foundation Server 2012 Builds: Understand, Configure, and Customize Benjamin Day.
Patterns in programming 1. What are patterns? “A design pattern is a general, reusable solution to a commonly occurring problem in software. A design.
© ALEXANDRE CUVA  VERSION 2.00 Test Driven Design.
@DNNCon Don’t forget to include #DNNCon in your tweets! Effective Unit Testing for DNN James McKee Solutions Developer / Enterprise
How to be a C# ninja in 10 easy steps Benjamin Day.
Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer Architecture.
Refactoring for Testability (or how I learned to stop worrying and love failing tests) Presented by Aaron Evans.
Zero to Hero: Untested to Tested with Visual Studio Fakes Benjamin
Copyright © 2005 Charlie Poole. All rights reserved Test First User Interfaces XP2005 Sheffield University June 18, 2005.
The Start Menu……..Exposed What you never knew existed.
Real World SQL Server Data Tools Benjamin
ASP.NET MVC An Introduction. What is MVC The Model-View-Controller (MVC) is an architectural pattern separates an application into three main components:
#SPSSAN June 30, 2012 San Diego Convention Center WRITING TESTABLE CODE In SharePoint.
Testing WebForms w/ Model-View-Presenter Erik Peterson.
Modern Development Technologies in SharePoint SHAREPOINT SATURDAY OMAHA APRIL, 2016.
Programming with MVVM Miguel A. Castro Architect -
Understanding Dependency Injection… and those pesky containers Miguel A. Castro Architect -
Benjamin Day Get Good at DevOps: Feature Flag Deployments with ASP.NET, WebAPI, & JavaScript.
Benjamin Unit Testing & Test-Driven Development for Mere Mortals.
Benjamin Day Role-based Security Stinks: Better Authorization in ASP.NET.
Presented by Alexey Vedishchev Developing Web-applications with Grails framework American University of Nigeria, 2016 Intro To MVC Architecture.
Beyond Basic Unit Testing: Mocks, Stubs, User Interfaces, and Refactoring for Testability Benjamin Day
Ognjen Bajić Ana Roje Ivančić Ekobit Efficient Application Testing.
Benjamin Day Real World Scrum with TFS 2015 & VSTS.
DevOps with ASP.NET Core and Entity Framework Core
Real world Windows Phone development
Better Unit Tests through Design Patterns: Repository, Adapter, Mocks, and more… Benjamin
Top 10 Mistakes in Unit Testing
CO6025 Advanced Programming
Unit Testing & Test-Driven Development for Mere Mortals
Unit Testing & Test-Driven Development for Mere Mortals
Get Good at DevOps: Feature Flag Deployments with ASP
Zero to Hero: Untested to Tested with Visual Studio Fakes
Unit Testing & Test-Driven Development for Mere Mortals
Real World Scrum with TFS & VSTS / Azure DevOps
2/24/2019 © 2014 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks.
Entity Framework Core for Enterprise Applications
Implementing Security in ASP.NET Core: Claims, Patterns, and Policies
Developing and testing enterprise Java applications
Presentation transcript:

Design for Testability: Mocks, Stubs, Refactoring, and User Interfaces Benjamin Day benday.com |

Introductions: 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

Agenda Quick ‘what’ and ‘why’ Dependency Injection Mocks & Stubs Databases, Web Services User Interface Testing

WHAT & WHY

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

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

Maximize 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

How would you test this?

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?

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

Try 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.

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.

MOCKS & STUBS

Mocks vs. Stubs vs. Dummies vs. Fakes Martin Fowler mocksArentStubs.html 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

RhinoMocks Dynamic Mocking Framework By Ayende Rahien mocks.aspx mocks.aspx Free under the BSD license

RhinoMocks Primer MockRepository Owns the mocking session StrictMock ()  Call order sensitive DynamicMock ()  Ignores call order Stub ()  Ignores Order Create get/set properties automatically ReplayAll() Marks start of the testing

Demo 1: Stub With RhinoMocks

Demo 2: Test Exception Handling Look at some existing code Refactor for testability Use RhinoMocks to trigger the exception handler

DATABASES & SERVICES

Avoid 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?

Reminder: Only test what you have to test. To test this latch, do you have to take the plane up for a spin?

The Repository Pattern “Mediates between the domain and data mapping layers using a collection-like interface for accessing domain objects.” Encapsulates the logic of getting things saved and retrieved

Person Repository

Demo 3: Repository Pattern Simplify database (or web service) unit test with a repository

USER INTERFACES

User Interfaces: The Redheaded Stepchild 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

My $0.02 Solve the problem by not solving the problem Find a way to minimize what you can’t automate

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

Design 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

Service 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

Model View Presenter (MVP)

The 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

View 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

The 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

Model View Presenter (MVP)

Designing the UI for Testability PersonDetailView.aspx

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

Demo 4 Refactor to UI Interfaces

Agenda Quick ‘what’ and ‘why’ Dependency Injection Mocks & Stubs Databases, Web Services User Interface Testing

Thank you. blog.benday.com | |