SlideShare a Scribd company logo
1 of 72
Modern Web Development
Testable WebForms with Model View Presenter
Modern Web Development
Testable WebForms with Model View Presenter
Rachel
developingUX.com
speakermix.com/calebjenkins
@calebjenkins
#ttf2014
why
Testable
Repeatable
Maintainable Reliable-able
Scalable
Extensible
Deliverable
Workable
Development
Object Orientation
SOLID
Patterns
Secure Coding
Engineering
Automated Tests
Source Control
Automated Builds
Process
Agile, Lean, XP
Team Dynamics
Continuous Learning
http://developingUX.com/TulsaTech2014/
1:30pm
3:00pm
4:00pm
#ttf2014
write better code
modern web!
back to…
15
what are the 4 big parts of unit tests?
Test Framework Test Runner
Code Tests
16
your application
what you want to test
your test code
the code that tests the
code that you wrote or are
going to write
attributes and asserts
the framework provides
the attributes and asserts
so we know what the tests
are doing.
Examples: nUnit jUnit
cppUnit
runs the tests
often associated with the
test framework; is
distinctly separate.
sometime integrated in
IDE, CI Server or stand
alone exe
Test Runner
17
Code
TestFramework
Tests
Tests
Tests
Tests
Tests
Test Runners
18
nUnit Test Runner
Visual Studio (VS Test)
CodeRush / ReSharper
Continuous Integration (Team City)
the problem with edges
UI DataBusiness
Edges are
Hard to Test
Testing edges
can be like
testing to see
if you’re good
at cliff jumping
That’s not me
..or you’re
stuff on a rock.
You’re either an
expert and it works…
UI DataData
Logic
UI
Logic
Business
Edges are
Hard to Test
Edges are still
Hard to Test
by separating UI/Data edges from
UI/Data logic we’re increasing the testable area
we’ve also made it easier to implement
various UI and Data platforms
without affecting the application logic
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)
Definitions
Reflects
With MVP
the Presenter “knows” about every field.
MobileNumber
BirthDay
In a MVVM
the View “binds” to the ViewModel
angularJS.org
handelbarsJS.com
knockoutJS.com
knockoutMVC.com ToDoMVC.com
WebForms -> MVP
WebForms -> MVP
interface IMyPageView
MyPage.aspx
class MyPage : Page, IMyPageView
event Load
bool isPostBack
string MyText
event ButtonPressed
class MyPagePresenter
MyPagePresenter
(IMyPageView view)
DetermineViewInteractions
1
implement Viewin Page
2
wire View in Presenter
3
Test Presenter
4
Did you say
Testable WebForms?
Resources & Frameworks
BDD
http://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
Handle your
dependencies
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 Silverlight
Wrox Press 2012
Caleb Jenkins
Data Access
Data Logic
Integration Service Proxy
App Domain Domain Validation
UI Logic
UI
How do you test this
with these
dependencies
Data Access
Data Logic
Integration Service Proxy
App Domain Domain Validation
UI Logic
UI
Test Runner
Test Code
Integration Service Proxy
App Domain Domain Validation
UI Logic
Dependency Injection + Interfaces
Faked dependencies to increase unit isolation
Leverage mocking frameworks makes life better
Note:
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
http://developingUX.com/DI/
- Real World .NET, C# and Silverlight
Wrox 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 in .NET
Microsoft.Fakes
Bringing DI together
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();
WebForms -> MVP
MyPage.aspxMyPagePresenter
Leverage DI from Here! Thin with no UI logic!
Non-Platform Specific in View Interface
WebForms -> MVP
Test the Presenter!
DI + Mocks == Awesome!
WireUp DI
5
Consider WebFormsMVP
6
6 http://WebFormsMVP.com
http://WebFormsMVP.com
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/
http://www.flickr.com/photos/lowfatbrains/80542761/
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/
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/
Please Complete An Evaluation Form
Your input is important!
You can access Evaluation Forms at:
http://TulsaTechFest.com
developingUX.com
speakermix.com/calebjenkins
@calebjenkins
Test Code … 3 A’s
75
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
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
Red
GreenRefactor
Test Runner
Simple Tests (return values) with no dependencies…
App Domain Domain Validation
UI Logic
Test Code
Unit Tests focus on a Unit
Test a unit in isolation from other units
Control input => Testable output
as a <role>,
I will <function>
so that <value>
Defining Behavior
Discussion
what if our “tests”
given <class> to test
when <setup> scenario
then <test> outcome
matched our
language?
Discussion
Discussion
BDD

More Related Content

More from Caleb Jenkins

10 Reasons Your Software Sucks - Election 2012 Edition
10 Reasons Your Software Sucks - Election 2012 Edition10 Reasons Your Software Sucks - Election 2012 Edition
10 Reasons Your Software Sucks - Election 2012 EditionCaleb Jenkins
 
Windows 8 & Phone 8 - an Architectural Battle Plan
Windows 8 & Phone 8 - an Architectural Battle PlanWindows 8 & Phone 8 - an Architectural Battle Plan
Windows 8 & Phone 8 - an Architectural Battle PlanCaleb Jenkins
 
Scaling Scrum with UX
Scaling Scrum with UXScaling Scrum with UX
Scaling Scrum with UXCaleb Jenkins
 
Scaling Scrum with UX
Scaling Scrum with UXScaling Scrum with UX
Scaling Scrum with UXCaleb Jenkins
 
Taming the Monster Legacy Code Beast
Taming the Monster Legacy Code BeastTaming the Monster Legacy Code Beast
Taming the Monster Legacy Code BeastCaleb Jenkins
 
Silverlight for Mobile World Dominations
Silverlight for Mobile World DominationsSilverlight for Mobile World Dominations
Silverlight for Mobile World DominationsCaleb Jenkins
 
.NET on the Cheap - Microsoft + OSS
.NET on the Cheap - Microsoft + OSS.NET on the Cheap - Microsoft + OSS
.NET on the Cheap - Microsoft + OSSCaleb Jenkins
 
10 practices that every developer needs to start right now
10 practices that every developer needs to start right now10 practices that every developer needs to start right now
10 practices that every developer needs to start right nowCaleb Jenkins
 
Threat Modeling - Writing Secure Code
Threat Modeling - Writing Secure CodeThreat Modeling - Writing Secure Code
Threat Modeling - Writing Secure CodeCaleb Jenkins
 
Dependency Injection in Silverlight
Dependency Injection in SilverlightDependency Injection in Silverlight
Dependency Injection in SilverlightCaleb Jenkins
 
Becoming A Presenter in the .NET World
Becoming A Presenter in the .NET WorldBecoming A Presenter in the .NET World
Becoming A Presenter in the .NET WorldCaleb Jenkins
 
Silverlight 2 with Visual Studio 2008 and Expression Blend
Silverlight 2 with Visual Studio 2008 and Expression BlendSilverlight 2 with Visual Studio 2008 and Expression Blend
Silverlight 2 with Visual Studio 2008 and Expression BlendCaleb Jenkins
 
ASP.NET AJAX with Visual Studio 2008
ASP.NET AJAX with Visual Studio 2008ASP.NET AJAX with Visual Studio 2008
ASP.NET AJAX with Visual Studio 2008Caleb Jenkins
 

More from Caleb Jenkins (14)

10 Reasons Your Software Sucks - Election 2012 Edition
10 Reasons Your Software Sucks - Election 2012 Edition10 Reasons Your Software Sucks - Election 2012 Edition
10 Reasons Your Software Sucks - Election 2012 Edition
 
Windows 8 & Phone 8 - an Architectural Battle Plan
Windows 8 & Phone 8 - an Architectural Battle PlanWindows 8 & Phone 8 - an Architectural Battle Plan
Windows 8 & Phone 8 - an Architectural Battle Plan
 
Scaling Scrum with UX
Scaling Scrum with UXScaling Scrum with UX
Scaling Scrum with UX
 
Coding Naked
Coding NakedCoding Naked
Coding Naked
 
Scaling Scrum with UX
Scaling Scrum with UXScaling Scrum with UX
Scaling Scrum with UX
 
Taming the Monster Legacy Code Beast
Taming the Monster Legacy Code BeastTaming the Monster Legacy Code Beast
Taming the Monster Legacy Code Beast
 
Silverlight for Mobile World Dominations
Silverlight for Mobile World DominationsSilverlight for Mobile World Dominations
Silverlight for Mobile World Dominations
 
.NET on the Cheap - Microsoft + OSS
.NET on the Cheap - Microsoft + OSS.NET on the Cheap - Microsoft + OSS
.NET on the Cheap - Microsoft + OSS
 
10 practices that every developer needs to start right now
10 practices that every developer needs to start right now10 practices that every developer needs to start right now
10 practices that every developer needs to start right now
 
Threat Modeling - Writing Secure Code
Threat Modeling - Writing Secure CodeThreat Modeling - Writing Secure Code
Threat Modeling - Writing Secure Code
 
Dependency Injection in Silverlight
Dependency Injection in SilverlightDependency Injection in Silverlight
Dependency Injection in Silverlight
 
Becoming A Presenter in the .NET World
Becoming A Presenter in the .NET WorldBecoming A Presenter in the .NET World
Becoming A Presenter in the .NET World
 
Silverlight 2 with Visual Studio 2008 and Expression Blend
Silverlight 2 with Visual Studio 2008 and Expression BlendSilverlight 2 with Visual Studio 2008 and Expression Blend
Silverlight 2 with Visual Studio 2008 and Expression Blend
 
ASP.NET AJAX with Visual Studio 2008
ASP.NET AJAX with Visual Studio 2008ASP.NET AJAX with Visual Studio 2008
ASP.NET AJAX with Visual Studio 2008
 

Modern Web - MVP Testable WebForms

Editor's Notes

  1. Build out slide.. Before you click through: take a minute to have student “read” test and talk out what it’s doing.