5 Domain Logic (“the code that makes you money”) Data LayerResponsible for talking to “persistence layer”PresentationWebDesktopServiceDomain Logic (“the code that makes you money”)Data / ModelPersistenceDatabaseCloudXMLEtc.
19 testing the data layer Everything is familiar up to this point. Now the dreaded step may of us avoid: Creating tests for the data layer
20 A B two testing camps Test Database Mock Database Concepts: Concepts: Test against “real” databaseUse set-up/tear-down code to create test dataGood When…You put lots of logic in your databaseConcepts:Test against “fake” databaseIsolates your code from database behaviorGood When…You want fast unit tests and you put most logic in code
21 picking a camp “Test” Database Mocking Pros Pros Cons Cons Unit test Isolate concernsFastCons“Hides” issuesDoes not test database logic“Test” DatabaseProsCatches more issuesFamiliarConsSlowNot a “unit” testHides issues example = concurrency, auto-gen DB values, etc.
22 testing considerations What is a unit test?What are other types of testing?
23 UNIT TEST INTEGRATION TEST FUNCTIONAL TEST “Isolated. Repeatable. Fast.”INTEGRATION TEST“Test interaction between units.”FUNCTIONAL TEST“Test behavior from user perspective.”
24 Database Communication (ORM, ADO.NET, etc.) mock testingGoal: Test your business logicUI BehaviorsServicesBusiness CodeRepositoryDatabase Communication (ORM, ADO.NET, etc.)Database
25 mocking Stunt doubles for real objects Look the same on the outside Mocking Tools:JustMock (by Telerik)Isolator (by TypeMock)MOQ (OSS)RhinoMocks (OSS)
26 AAA mocking pattern//Arrange Set-up your test variables and mocks //Act Execute your code like normal //Assert Verify what happened
28 A Steps for every test: test database Goal: Test your business logic + database behaviorSteps for every test:Create database schema + test data(Optional) Test database setup correctlyExecute unit test codeVerify database behaved correctly
29 hard parts Creating test schema/data Speed DbUnit In memory database SQL Lite, SQL CE, etc.
30 rules for test database tests Prior to running tests, schema should be redeployed to test DB (+ test data)Tests should not change existing dataEdits, Deletes should be on records created by testOriginal data should be read-onlyTests should not depend on changes from previous tests
31 DEMO: TESTinG with real DB Creating integration tests to talk to real databaseDEMO: TESTinG with real DB
34 Links4GuysFromRolla on Testing DAL (2005)Unit Testing the DAL (Java, but great discussion of DAL data testing)Roy Osherove on using Mocks for DAL testingSQL Lite project pageSystem.Data.SQLite:DbUnit.NET (last updated 2006 – still alpha)SQL Server Compact 4 CTP1 (2010)SQL Script to clear all tables in databaseMicrosoft.SqlServer.Management.Smo primerMulti-tier L2S architecture ideas