/
Beyond Basic Unit Testing: Beyond Basic Unit Testing:

Beyond Basic Unit Testing: - PowerPoint Presentation

phoebe-click
phoebe-click . @phoebe-click
Follow
366 views
Uploaded On 2018-10-21

Beyond Basic Unit Testing: - PPT Presentation

Mocks Stubs User Interfaces amp Refactoring for Testability Benjamin Day Who am I Owner Benjamin Day Consulting Inc Email bendaybendaycom Web httpwwwbendaycom Blog httpblogbendaycom ID: 692090

business test unit testing test business testing unit pattern benday view code mvp http tier model driven team interface

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "Beyond Basic Unit Testing:" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

Slide1

Beyond Basic Unit Testing: Mocks, Stubs, User Interfaces, &Refactoring for Testability

Benjamin DaySlide2

Who am I?Owner, Benjamin Day Consulting, Inc.

Email: benday@benday.com

Web:

http://www.benday.com

Blog: http://blog.benday.comTrainer, ConsultantVisual Studio Team System, Team Foundation ServerMicrosoft MVP for VSTSMicrosoft VSTS/TFS Customer Advisory CouncilMicrosoft Cloud Services Advisory GroupLeader of Beantown.NET INETA User GroupSlide3

Shameless PlugThis presentation is based on content from my training courses.Unit Testing & Test-Driven Development with Visual

Studio (2 days)Visual Studio Team System & Team Foundation Server (5 days)

Public course schedule at

http://benday.com

On-site training and consulting, too.Copyright © 2007, Benjamin Day Consulting, Inc. www.benday.com3Slide4

OverviewWhy do you care about unit testing?Testing ProblemsTesting Solutions

 CodeSlide5

What & Why?Slide6

What is Test Driven Development?Way of developing code so that you always have proof that something is working

Code that validates other codeSmall chunks of “is it working?”Small chunks = Unit TestsKent Beck (“Test-Driven Development”, Addison-Wesley) says “Never write a single line of code unless you have a failing automated test.”Slide7

What is a Unit Test?Wikipedia says, “a unit test is a procedure used to validate that a particular module of source code is working properly”Method that exercises another piece of code and checks results and object state using assertionsSlide8

Why Use TDD?High-quality code Fewer bugs

Bugs are easier to diagnose“Test First” method

 ~up-front design

Less time spent in the debuggerTests that say when something works Easier maintenance  easier refactoringSelf documentingSlide9

Maximize Your QA StaffYou shouldn’t need QA to tell you your code doesn’t workUnit testing to minimizes the pointless bugs

“nothing happened” “I got an error message” + stack trace

NullReferenceException

QA should be checking for:

Meets requirementsUsability problemsVisual things (colors, fonts, etc)When you get a bug assigned to you it should add business valueSlide10

Problems & SolutionsSlide11

Testing ProblemsExtra code just for the sake of the testCode coverage on exception handlingTendency toward end-to-end integration tests

DatabasesWCF & Services

Big back-end systems

Unit testing UIsSlide12

Testing SolutionsMocks, Stubs, and Mocking FrameworksInterface-driven codingFactory Pattern and/or

IoC FrameworksRepository PatternModel-View-Presenter (MVP) Pattern

Model-View-Controller (MVC) PatternSlide13

Mocks vs Stubs vs Dummies vs

Fakes

Martin Fowler

http://martinfowler.com/articles/

mocksArentStubs.htmlDummy = passed but not usedFake = “shortcut” implementationStub = Only pretends to work, returns pre-defined answerMock = Used to test expectations, requires verification at the end of testSlide14

RhinoMocksDynamic Mocking FrameworkBy Ayende

Rahien http://ayende.com/projects/rhino-mocks.aspx

Free under the BSD license Slide15

RhinoMocks PrimerMockRepository

Owns the mocking sessionStrictMock<T>()

 Call order sensitive

DynamicMock

<T>()  Ignores call orderStub<T>()  Ignores OrderCreate get/set properties automaticallyReplayAll()Marks start of the testingSlide16

Demo 1: Stub With RhinoMocksSlide17

Demo 2: Test Exception HandlingLook at some existing codeRefactor for testability

Use RhinoMocks to trigger the exception handlerSlide18

Avoid End-to-End Integration TestsDoes 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?Slide19

The 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 retrievedSlide20

Person RepositorySlide21

Demo 3: Repository PatternSimplify database (or web service) unit test with a repositorySlide22

User Interface TestingSlide23

User Interfaces: The Redheaded Stepchild of the Unit Testing WorldNot easy to automate the UI testingBasically, automating button clicks

UI’s almost have to be tested by a humanComputers 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 applicationChecking that fields have the right dataChecking field visibilitySlide24

VSTS Web TestsRecord paths through your applicationFills in form valuesClick buttons

ValidatesDifficult to do test-driven developmentSlide25

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

The Solution.Keep as much logic as possible out of the UIShouldn’t be more than a handful of assignments

Nothing smartReal work is handled by the business tierTest the business tier

“Transaction Script” Pattern

“Domain Model” Pattern

“Service Layer” Pattern“Model View Presenter” Pattern“Model View Controller” PatternSlide27

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 StaffordSlide28

Model View Presenter (MVP)Slide29

Model View Presenter (MVP)Slide30

The Common Tiers

Presentation tierASP.NET

Windows Forms

WPF

WCF Service The “View” of MVPPresenter TierHandles the "conversation" between the presentation tier implementation and the business tierDefines 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 ServerSlide31

Tiering Up: Keep Logic Out Of The UIsBusiness Object Tier (Domain Model pattern)

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

Create an interface to represent each page in your applicationCreate Editor Facades as an adapter between the UI interfaces and the business objectsCopyright © 2007, Benjamin Day Consulting, Inc. www.benday.com31Slide32

View interfacesInterface represents the fields manipulated through the UI

ASPX Page or Windows Form Implements the interfaceInterface’s properties wrap UI widgets

ICustomerDetail.CustomerName

 m_textboxCustomerName.TextUse a stub represent the UIWrite unit tests to test the functionality of the presenterAvoid business objects  favor scalarsSlide33

The PresenterService Layer PatternWrap larger operations that are relevant to each UI page/screen interface

InitializeBlank(ICustomerDetail)

View(

ICustomerDetail

)Save(ICustomerDetail)Since each page implements the interface, pass the page reference to the facadeSlide34

Model View Presenter (MVP)Slide35

Designing the UI for Testability

PersonDetailView.aspxSlide36

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 objectsDoesn’t know about any details of loading or savingDoesn’t have to know about validation

All this logic goes into the editor façade and testable via unit test Slide37

Avoid Referencing Business Objects in the UI “interfaces”ASP.NETEasier to write stateless pages (everything is in ViewState)

No need to try to re-create complex objects in code behind in order to saveSlide38

Demo 4Refactor to UI InterfacesSlide39

Summary: Testing SolutionsMocks, Stubs, and Mocking FrameworksInterface-driven codingFactory Pattern and/or

IoC FrameworksRepository Pattern

Model-View-Presenter (MVP) PatternSlide40

Who am I?Owner, Benjamin Day Consulting, Inc.

Email: benday@benday.com

Web:

http://www.benday.com

Blog: http://blog.benday.comTrainer, ConsultantVisual Studio Team System, Team Foundation ServerMicrosoft MVP for VSTSMicrosoft VSTS/TFS Customer Advisory CouncilMicrosoft Cloud Services Advisory GroupLeader of Beantown.NET INETA User Group