C# TDD Painkillers@Ben_HallBen@BenHall.me.ukBlog.BenHall.me.ukCodeBetter.com/blogs/BenHallhttp://www.flickr.com/photos/pinksherbet/4004791663/
London based ASP.net MVPWeb Developer @ 7digital.com Open Source DeveloperWHO AM I?Co-Author Testing ASP.net Web Applicationswww.testingaspnet.com
YouTest DesignApp Design
http://www.flickr.com/photos/chiotsrun/4115059294/sizes/l/
Test Designhttp://www.flickr.com/photos/nyuhuhuu/4442144329/
TDD doesn’t mean NUnit
TDD is about learning
TDD is about validating the unknownsProving your own assumptions
Fundamentally the biggest TDD painkiller is not doing TDD
That’s why we have a QA department
Ideally PairingDeveloper writing the testDeveloper writing the implementation
Do enough just to get the test to pass
Write more tests to express more behaviour
Wouldn’t it be cool if...
Loose focus
Takes longer to achieve what you wanted to deliverDo the simplest thing possible.“First make it work, then make it right”
“TDD like you meant it”
http://www.flickr.com/photos/peasap/1625639532/sizes/l/Spike
IRB (Ruby)Want rapid feedbackAllows you to learn quickly
Spikeshttp://github.com/BenHall/CastleDynamicProxy101/blob/master/CastleDynamicProxy101/Program.cshttp://github.com/BenHall/memcached101/blob/master/MemcacheDemo/Program.cs
NUnit makes learning repeatableHelps guide the learning during implementation
Test Naming and structurePain killer
Each test should add something to explain the behaviour of the class under testAs you read down then it should continue to add more information in a logical wayStructure
Bad structure    Method1_does_this    Method2_does_this    Method2_does_something_else    Method1_does_not_do_thisEXAMPLE
Bad structure    Method1_does_this    Method1_does_not_do_this    Method2_does_this    Method2_does_something_elseEXAMPLE
Context \ SpecTaken from RubyExample if the Persistent DLR integration
Naming of test ImplementationI follow a BDD style which explains the behaviourIf your naming talks about implementation, then your likely to test implementation – this could change! Like comments, you don’t want the test naming to be out of sync with what the actual test is doing
Actual test implementationPainkiller
Make sure the important details are easy to identifyHide the irrevent information to identify which bits are keyVariable names can help with thisvarexpiredCreditCardDate = ...Highlight relevant data
Duplicated Setup in testUse SetupMake test helpers which have pre-defined stub configsEXAMPLE
Duplicated Setup in testUse SetupMake test helpers which have pre-defined stub configsEXAMPLE
Tests are documentation
IT DEPENDS
Given, When, Then
Scenario: Payment method has expired   Given user John   And John has expired credit card   When I get the default payment method   Then an invalid credit card will be returnedCuke4nuke \ SpecFlow
Design smells indicate it’s not done via TDD
Random, Dates, Machine SettingsDateTime.Now();new Random().Next();
DateTimefuncRandom funcUse staticFunc<T>
EXTERNAL DEPENDENCIEShttp://www.flickr.com/photos/gagilas/2659695352/
Fake the other side
Mock External Dependencies
Anti Corruptionhttp://www.flickr.com/photos/aresauburnphotos/2678453389/sizes/l/
Mock UsageNot doing Unit Testing if you don’t use mocksOver usageASP.net MVC is perfect example of having to do thisStubs return stubs returning stubsToo many mocksMocking things you don’t ownHttpContextBaseIDbConnection
http://ayende.com/Blog/archive/2006/07/02/DoNOTMockSystemData.aspx
Fragile to refactoringFocusing too much on implementationNeeds to be behaviour drivenIncorrect Responsbilities
Running away from HttpContext
Interfaces establish behaviour boundaries. Stub\Mock at boundaries
Let the tests drive the designScares some people. But again, it’s a learning tool so let it educate you!
http://www.flickr.com/photos/yakobusan/2436481628/Application Design
DependenciesIoC – FearTDD + IoC + Dependencies can lead to a very different application designFear is mainly because
Poor man’s IoC
REALLY?!?
Design SmellResponsibilities of the class being testedFixture talks about lots of different conceptsContext \ Spec makes this a lot easier to identifyEasier to see if structured correctlySmell - Responsibilitieshttp://www.flickr.com/photos/ricmcarthur/56268640/
Catalogue HandlerTests help indicate smells
Catalogue HandlerTests help indicate smellsAbstract away?Combined service?
Single responsibility principleOpen/closed principleLiskov substitution principleInterface segregation principleDependency inversion principle
UI Testinghttp://blig.ig.com.br/brokenarrow/files/2009/04/too_many_toolbars.jpg
WPF Apps With The Model-View-ViewModel Design PatternMVVM Patternhttp://msdn.microsoft.com/en-us/magazine/dd419663.aspx
MVP Pattern
MVP Pattern
Acceptance Test Driven Development
http://codebetter.com/blogs/benhall/archive/2010/03/16/testing-a-wpf-ui-using-ruby-cucumber-and-wipflash-dll.aspx
Scenario: Saving a product by name only      Given the application has started      And I enter the name "Product 1"      When I save the product      Then "Product 1" should be available to purchase 
 Given /^the application has started$/ do    host = ApplicationLauncher.new    @app = host.launch Dir.pwd + '/src/ExampleUIs/bin/Debug/ExampleUIs.exe'    @main_window = @app.find_window 'petShopWindow'  end    Given /^I enter the name "([^\"]*)"$/ do |product_name|    textbox = @main_window.find_TextBox "petNameInput"    textbox.text = product_name  end    When /^I save the product$/ do    button = @main_window.find_Button "petSaveButton"    button.click    sleep(1)  end    Then /^"([^\"]*)" should be available to purchase$/ do |product_name|    combo = @main_window.find_ComboBox "basketInput"    combo.Items.should include("Pet[#{product_name}]")  end    After do |scenario|    @app.process.kill  end  
Full stack testing for ATDD?Waste of time! Feature coverageCan be used as part of a test metric
What’s good application design?When you read the code, it’s what you expectHow quickly you can add testsHow quickly you can implement new functionalityUnderstandable by the teamHow much code churn\rework is being performedHow much test coverage
Code CoverageWaste of time! Feature coverageCan be used as part of a test metric
Code and Test ReviewsBetter than metricsWhat the team thinks is good
7digital XTCR – Cross Team Code ReviewAt 7digital
It’s all about the mindset
Hardest part == getting started
Second hardest part == keep doing it
Becomes natural
Pair with someonehttp://www.flickr.com/photos/jasohill/65804069/sizes/l/
Katahttp://www.flickr.com/photos/53511700@N06/4944542750/sizes/l/

TDD Painkillers