modern web - mvp testable webforms

72
Modern Web Development Testable WebForms with Model View Presenter

Upload: caleb-jenkins

Post on 10-May-2015

163 views

Category:

Software


2 download

DESCRIPTION

The world is moving towards ASP.NET MVC.. but what about your legacy WebForms development. What are the things you can do today to make your WebForms more testable, reliable and even increase the SEO and usability of your WebForms. This talk will walk through applying the Model View Presenter pattern to your ASP.NET WebForm applications and introduce you to some additional enhancements that Microsoft has made to WebForms recently to make your site and life that much better! This presentation was given at the Tulsa Tech Fest 2014 - in Tulsa, OK - http://developingux.com/TulsaTech2014/

TRANSCRIPT

Page 1: Modern Web - MVP Testable WebForms

Modern Web DevelopmentTestable WebForms with Model V iew Presenter

Page 2: Modern Web - MVP Testable WebForms

Modern Web DevelopmentTestable WebForms with Model V iew Presenter

Page 3: Modern Web - MVP Testable WebForms

Please help us!Thank our Sponsors:

Page 4: Modern Web - MVP Testable WebForms

Caleb

Page 5: Modern Web - MVP Testable WebForms

Rachel NoahEmily

Kaitlyn

Abigail

Page 6: Modern Web - MVP Testable WebForms

Caleb

developingUX.comspeakermix.com/calebjenkins

@calebjenkins#ttf2014

Page 7: Modern Web - MVP Testable WebForms

why

TestableRepeatable

Maintainable Reliable-able

ScalableExtensible

Deliverable

Workable

Page 8: Modern Web - MVP Testable WebForms

Development

Object Orientation

SOLID

Patterns

Secure Coding

Building teams that engineer better software

D e v e l o p m e n t ∙ E n g i n e e r i n g ∙ P r o c e s s

Page 9: Modern Web - MVP Testable WebForms
Page 10: Modern Web - MVP Testable WebForms

http://developingUX.com/TulsaTech2014/

1:30pm

3:00pm

4:00pm

#ttf2014

Page 11: Modern Web - MVP Testable WebForms

write better code

Page 12: Modern Web - MVP Testable WebForms

modern web!

back to…

Page 13: Modern Web - MVP Testable WebForms

15

what are the 4 big parts of unit tests?what are the 4 big parts of unit tests?

Page 14: Modern Web - MVP Testable WebForms

16

Test Framework Test Runner

Code Testsyour applicationwhat you want to test

your test codethe code that tests the

code that you wrote or are going to write

attributes and assertsthe framework provides

the attributes and asserts so we know what the tests

are doing. Examples: nUnit jUnit

cppUnit

runs the testsoften associated with the

test framework; is distinctly separate.

sometime integrated in IDE, CI Server or stand

alone exe

Page 15: Modern Web - MVP Testable WebForms

17

Test Runner

Code

Test

Fra

mew

orkTests

Tests

Tests

Tests

Tests

Page 16: Modern Web - MVP Testable WebForms

18

Test Runners

nUnit Test RunnerVisual Studio (VS Test)

CodeRush / ReSharperContinuous Integration (Team City)

Page 17: Modern Web - MVP Testable WebForms

the problem with edges

Page 18: Modern Web - MVP Testable WebForms

UI DataBusiness

Edges areHard to Test

Page 19: Modern Web - MVP Testable WebForms

Testing edges can be like

testing to see if you’re good at cliff jumping

Page 20: Modern Web - MVP Testable WebForms

That’s not me

Page 21: Modern Web - MVP Testable WebForms

..or you’restuff on a rock.

You’re either an expert and it works…

Page 22: Modern Web - MVP Testable WebForms

UI DataDataLogic

UILogicBusiness

Edges areHard to Test

Edges are stillHard to Test

by separating UI/Data edges fromUI/Data logic we’re increasing the testable area

we’ve also made it easier to implementvarious UI and Data platforms

without affecting the application logic

MVP, MVC

or MVVM

Patterns

Repository

Pattern

Page 23: Modern Web - MVP Testable WebForms

25

Model View Controller (MVC)•All input is routed to a controller•Example Web Scenarios•ASP.NET MVC Framework

Model View Presenter (MVP)• View initiates Presenter• UI Logic is contained in Presenter• Example WinForms & ASP.NET Webform apps

Model View ViewModel (MVVM)• ViewModel is a view specific model• VM is can mash up application models• UI logic contained in ViewModel• Example Rich Data binding Scenarios

(WPF / Silverlight / XAML / JS)

UI Patterns for

Testability

Keep Views Dumb

Each Pattern has a

testable part

Page 24: Modern Web - MVP Testable WebForms

View

Controller

Presenter (UI Logic)

Model (Data)

You!

Definitions

(IView)

Page 25: Modern Web - MVP Testable WebForms

V

P

MMVP

Request

RendersEvents

Updates

Gets

Updates

Page 26: Modern Web - MVP Testable WebForms

V

C

MMVC

Request

Up

dates

Reflects

Ren

ders

Page 27: Modern Web - MVP Testable WebForms

V

MM-V-VM

Request

RendersEvents

Updates

Gets/Updates

VM

Page 28: Modern Web - MVP Testable WebForms

P

M (Customer)

With MVP

Request

Renders

the Presenter “knows” about every field.

FirstName

LastName

GenderMobileNumber

BirthDayEmail

WebpagePhotoAboutM

(OrderHistory)OrderDetails[ ]

Page 29: Modern Web - MVP Testable WebForms

M (Customer)

In a MVVM

Request

Renders

M (OrderHistory)

the View “binds” to the ViewModel

VM

Page 30: Modern Web - MVP Testable WebForms
Page 31: Modern Web - MVP Testable WebForms

angularJS.org

handelbarsJS.comknockoutJS.com

knockoutMVC.com ToDoMVC.com

Page 32: Modern Web - MVP Testable WebForms

WebForms -> MVP

Page 33: Modern Web - MVP Testable WebForms

WebForms -> MVP

interface IMyPageView

MyPage.aspx class MyPage : Page, IMyPageView

event Loadbool isPostBackstring MyTextevent ButtonPressed

class MyPagePresenterMyPagePresenter (IMyPageView view)

wire these up!

Page 34: Modern Web - MVP Testable WebForms

Determine View Interactions

1

Page 35: Modern Web - MVP Testable WebForms

implement View in Page

2

Page 36: Modern Web - MVP Testable WebForms

wire View in Presenter

3

Page 37: Modern Web - MVP Testable WebForms

Test Presenter

4

Page 38: Modern Web - MVP Testable WebForms

Did you sayTestable WebForms?

Page 39: Modern Web - MVP Testable WebForms

Resources & Frameworks

BDDhttp://neelnarayan.blogspot.com/2010/07/bdd-is-more-than-tdd-done-right.html

more than TDD done right

http://dannorth.net/introducing-bdd/

introducing BDD

http://lucisferre.net/2011/02/05/behavior-driven-test-driven-domain-driven-design/

behavior driven, test driven, domain driven

nBehave, nSpec, SpecFlow, StoryQ, mSpec, StorEvil

Page 40: Modern Web - MVP Testable WebForms
Page 41: Modern Web - MVP Testable WebForms

Handle your dependencies

Page 42: Modern Web - MVP Testable WebForms

Dependencies“The single greatest thing that you can do to make your code more testable and healthy is to start taking a Dependency Injection approach to writing software”

Dependencies“The single greatest thing that you can do to make your code more testable and healthy is to start taking a Dependency Injection approach to writing software”

- Real World .NET, C# and SilverlightWrox Press 2012

Caleb Jenkins

- Real World .NET, C# and SilverlightWrox Press 2012

Caleb Jenkins

Page 43: Modern Web - MVP Testable WebForms

?Data Access

Data Logic

Integration Service Proxy

App Domain Domain Validation

UI Logic

UI

How do you test this

with thesedependencies

Page 44: Modern Web - MVP Testable WebForms

Data Access

Data Logic

Integration Service Proxy

App Domain Domain Validation

UI Logic

UI

Page 45: Modern Web - MVP Testable WebForms

Test Runner

Test Code

Integration Service Proxy

App Domain Domain Validation

UI LogicFaked Input

Faked

Dependencies

Dependency Injection + InterfacesFaked dependencies to increase unit isolationLeverage mocking frameworks makes life better

Page 46: Modern Web - MVP Testable WebForms

Note:Dependency InjectionNote:Dependency Injection

will turn you in to a complete coding Ninja, however the full scope of DI with any of the many DI frameworks is beyond the scope of this talk

will turn you in to a complete coding Ninja, however the full scope of DI with any of the many DI frameworks is beyond the scope of this talk

Page 47: Modern Web - MVP Testable WebForms

http://developingUX.com/DI/

Page 48: Modern Web - MVP Testable WebForms

- Real World .NET, C# and SilverlightWrox Press 2012

Caleb Jenkins

- Real World .NET, C# and SilverlightWrox Press 2012

Caleb Jenkins

Mocking Framework“A mocking framework allows you to create fake classes on the fly in-line with your test code. That is a bit of a simplification, mocking frameworks use a combination of emits, reflection and generics to create run-time instance implementations of .NET Interfaces – whew, that’s a mouthful - it’s a whole lot easier to say that they create fake classes on the fly!”

Mocking Framework“A mocking framework allows you to create fake classes on the fly in-line with your test code. That is a bit of a simplification, mocking frameworks use a combination of emits, reflection and generics to create run-time instance implementations of .NET Interfaces – whew, that’s a mouthful - it’s a whole lot easier to say that they create fake classes on the fly!”

Page 49: Modern Web - MVP Testable WebForms

Mocking in .NETMicrosoft.Fakes

Page 50: Modern Web - MVP Testable WebForms

Bringing DI togetherBringing DI together

Page 51: Modern Web - MVP Testable WebForms

IData mockData = MockRepository.GenerateMock<IData>();

mockData.Expect(x => x.getAll<account>()).Return(sampleAccounts).Repeat.Once();

IAccountServices accountService= new AcmeAccountService(mockData);

var act = accountService.GetAccount(known_account_id);

mockData.VerifyAllExpectations();

using Rhino.MocksExpectations

DIaction

asserts

Page 52: Modern Web - MVP Testable WebForms

WebForms -> MVP

MyPage.aspxMyPagePresenter

Leverage DI from Here!Thin with no UI logic!Non-Platform Specific in View Interface

Page 53: Modern Web - MVP Testable WebForms

WebForms -> MVP

Test the Presenter!

DI + Mocks == Awesome!

Page 54: Modern Web - MVP Testable WebForms

Wire Up DI

5

Page 55: Modern Web - MVP Testable WebForms

Consider WebFormsMVP

6

Page 56: Modern Web - MVP Testable WebForms

6 http://WebFormsMVP.com

Page 57: Modern Web - MVP Testable WebForms

http://WebFormsMVP.com

Page 58: Modern Web - MVP Testable WebForms
Page 59: Modern Web - MVP Testable WebForms

Special Thanks!

http://developingUX.com/

psa

Page 60: Modern Web - MVP Testable WebForms

PROACTION

M E N T O R S

http://www.flickr.com/photos/jforth/5768064504/

http://www.flickr.com/photos/laughingsquid/255915238/

http://www.flickr.com/photos/dieselbug2007/370557683/

http://www.flickr.com/photos/m0php/530526644/

Photo Creditshttp://www.flickr.com/photos/lowfatbrains/80542761/

Page 61: Modern Web - MVP Testable WebForms

PROACTION

M E N T O R S

http://www.flickr.com/photos/georgivar/4974112941/

http://www.flickr.com/photos/redbettyblack/395899686/sizes/

http://www.flickr.com/photos/goldberg/815408116/

http://www.flickr.com/photos/fudj/122371431/

http://www.flickr.com/photos/yardsale/4524101944/

http://www.flickr.com/photos/38738277@N04/3652658961/

Page 62: Modern Web - MVP Testable WebForms

PROACTION

M E N T O R S

http://www.flickr.com/photos/utslibrary/6776175796/

http://www.flickr.com/photos/48725518@N03/4478990651/Copyright © Merriswheel – Used without permission

http://www.flickr.com/photos/mworrell/266913194/

https://www.flickr.com/photos/sprengben/4419536377/

Page 63: Modern Web - MVP Testable WebForms

PROACTION

M E N T O R S

Please Complete An Evaluation FormYour input is important!

You can access Evaluation Forms at:

http://TulsaTechFest.com

Fill them out!

You can win additional prizes!

Like a $50 Best Buy Gift Card!!

Winner drawn – Midnight, Sun Aug 17th!

Page 64: Modern Web - MVP Testable WebForms

PROACTION

M E N T O R S

Please help us!Thank our Sponsors:

Page 65: Modern Web - MVP Testable WebForms

Thank

you!

developingUX.comspeakermix.com/calebjenkins

@calebjenkins

Awesome

Reviews go

here!

I t ’s t ime for quest ions now

Page 66: Modern Web - MVP Testable WebForms

75

Test Code … 3 A’s

Arrange– Set up the scenario and the initial input values.

Often in a common [TestFixtureSetup] or [Setup] method

Act - Action that creates the outcome that is being tests,

usually calling some method in your code to test the result.

Assert – Is a boolean statement to your testing framework

to declare the expected outcome. Results in Pass or Fail

Arrange Act Assert

Page 67: Modern Web - MVP Testable WebForms

Data Access

Data Logic

Integration Service Proxy

App Domain Domain Validation

UI Logic

UI

Building better Lego’s

Tests are small

Tests are fast

Tests focus on one thing

Page 68: Modern Web - MVP Testable WebForms

Red

GreenRefactor

Page 69: Modern Web - MVP Testable WebForms

Test Runner

Simple Tests (return values) with no dependencies…

App Domain Domain Validation

UI Logic

Test Code

Faked Input

Unit Tests focus on a UnitTest a unit in isolation from other unitsControl input => Testable output

Page 70: Modern Web - MVP Testable WebForms

as a <role>,I will <function>so that <value>

Defining Behavior

Look

Familiar?

Discussion

Page 71: Modern Web - MVP Testable WebForms

what if our “tests”

given <class> to test

when <setup> scenario

then <test> outcome

matched ourlanguage?

Discussion

Page 72: Modern Web - MVP Testable WebForms

Discussion

BDD