Coding Naked Those Hard to Reach Places

Caleb Jenkins Technology Leader Quicken Loans

#ITDEVCONNECTIONS | ITDEVCONNECTIONS.COM Coding Naked! Unit Testing those hard to reach places!

@calebjenkins developingUX.com #CodeMash speakerpedia.com/speakers/caleb-jenkins of our clients would recommend us to others. *

github.com/quickenloans rockframework.github.io

* industry average is 11%

on refi or closing fees, plus work with our exclusive internal team! Tuesday

Wednesday

Wednesday

#ITDEVCONNECTIONS | ITDEVCONNECTIONS.COM Automated Hard to Reach Unit Test Places Fundamentals back to…

Coding Naked Is TDD only for coding elites? or something for every developer? How do we make Automated Tests the norm? make coding without tests as uncomfortable as coding naked * this is not me what are the 4 big parts of unit tests?

15 your test code your application the code that tests the what youCode want to test code that Testsyou wrote or are going to write

attributes and asserts runs the tests the framework provides often associated with the the attributes and asserts test framework; is soTest we know Framework what the tests distinctlyTest Runner separate. are doing. sometime integrated in Examples: nUnit jUnit IDE, CI Server or stand cppUnit alone exe 16 Test Runner Tests Tests Code Tests Tests

Tests Framework Test

17 Start A Test Framework B Test Runner C Test Code Test Framework

19 github.com/tpierrain/NFluent

github.com/erichexter/Should

#ITDEVCONNECTIONS | ITDEVCONNECTIONS.COM Test Runners

nUnit Test Runner Visual Studio (VS Test)

CodeRush / ReSharper

21 Continuous Integration (Team City) #ITDEVCONNECTIONS | ITDEVCONNECTIONS.COM 3 A’s Test Code

Arrange Act Assert

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

22 #ITDEVCONNECTIONS | ITDEVCONNECTIONS.COM Building better Lego’s

UI

UI Logic Tests are small App Domain Domain Validation Tests are fast Integration Service Proxy Tests focus on one thing Data Logic Data Access

#ITDEVCONNECTIONS | ITDEVCONNECTIONS.COM #ITDEVCONNECTIONS | ITDEVCONNECTIONS.COM #ITDEVCONNECTIONS | ITDEVCONNECTIONS.COM Red

Refactor Green

#ITDEVCONNECTIONS | ITDEVCONNECTIONS.COM Simple Tests return values no dependencies…

Test Runner Test Code

UI Logic App Domain Domain Validation Unit Tests focus on a Unit Test a unit in isolation from other units Control input => Testable output

#ITDEVCONNECTIONS | ITDEVCONNECTIONS.COM VSTest Start

Test Framework Test Runner Test Code

Set the Scene Discussion Defining Behavior

as a , I will so that Discussion what if our “tests” given to test when scenario then outcome matched our language? Discussion BDD Resources & Frameworks BDD more than TDD done right http://neelnarayan.blogspot.com/2010/07/bdd-is-more-than-tdd-done-right. behavior driven, test driven, domain driven http://lucisferre.net/2011/02/05/behavior-driven-test-driven-domain-driven-design/ nBehave, nSpec, SpecFlow, StoryQ, mSpec, StorEvil introducing BDD http://dannorth.net/introducing-bdd/

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 Harder to Test How do you test this

UI UI Logic

App Domain Domain Validation

Integration Service Proxy Data Logic with these Data Access dependencies

#ITDEVCONNECTIONS | ITDEVCONNECTIONS.COM UI UI Logic

App Domain Domain Validation

Integration Service Proxy Data Logic Data Access

#ITDEVCONNECTIONS | ITDEVCONNECTIONS.COM Test Runner

Test Code

UI Logic

App Domain Domain Validation Integration Service Proxy

Dependency Injection + Interfaces Faked dependencies to increase unit isolation Leverage mocking frameworks makes life better #ITDEVCONNECTIONS | ITDEVCONNECTIONS.COM 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/ http://developingUX.com/DI/ 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!”

- Real World .NET, C# and Silverlight Wrox Press 2012 Caleb Jenkins .Fakes Bringing DI together IData mockData = MockRepository.GenerateMock(); mockData.Expect(x => x.getAll()) .Return(sampleAccounts).Repeat.Once();

IAccountServices accountService = new AcmeAccountService(mockData); var act = accountService.GetAccount(known_account_id); mockData.VerifyAllExpectations(); Start (Framework, Runner, Test Code)

Set the Scene

Handle your Dependencies the problem with edges Edges are Hard to Test

UI Business Data Testing edges can be like testing to see if you’re good at cliff jumping That’s not me You’re either an expert and it works…

..or you’re stuff on a rock. EdgesEdges are are still Hard to Test

UI BusinessUI Data Data LogicLogic

we’veby separating also made UI/Datait easier edgesto implement from UI/Data logicvarious we’re UI andincreasing Data platforms the testable area without affecting the application logic

Edges are all around us UI Disk IODisk Network

Data

#ITDEVCONNECTIONS | ITDEVCONNECTIONS.COM Edges are all around us Network

#ITDEVCONNECTIONS | ITDEVCONNECTIONS.COM Edges are all around us INetwork Proxy Retry Logic / Formatting

#ITDEVCONNECTIONS | ITDEVCONNECTIONS.COM UI Disk IODisk Network

Data

#ITDEVCONNECTIONS | ITDEVCONNECTIONS.COM https://github.com/calebjenkins/Acme.CodingNaked These examples are for the expressed purpose of demonstrating unit testing scenarios.. none of this code (especially the supporting non-testing code) is intended to be deployed in production scenarios. This is just some code from a talk at some conference or user group. There's no warranty, express or implied. Works on my machines. I don't know you and I don't know how you got here. Stop calling. I no live here, you no call back!

“Don’t be silly” - Roy Osherove The Art of Unit Testing pg. 77 the problem with statics shared state be like..

IDependency

Idependency Method

Idependency Class to Test Extension Method

Idependency Method

the challenges with Microsoft Fakes

Identify your edges

Keep the edges dumb

Stop coding naked Start Framework, Runner, Test Code Set the Scene Handle your Dependencies no, seriously

http://www.flickr.com/photos/lowfatbrains/80542761/ 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/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 @calebjenkins developingUX.com #ITDevConnections #ITDEVCONNECTIONS | ITDEVCONNECTIONS.COM