5Domain Logic (“the code that makes you money”) Data LayerResponsible for talking to “persistence layer”PresentationWebDesktopServiceDomain Logic (“the code that makes you money”)Data / ModelPersistenceDatabaseCloudXMLEtc.
19testing the data layer Everything is familiar up to this point. Now the dreaded step may of us avoid: Creating tests for the data layer
20A 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
21picking 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.
22testing considerations What is a unit test?What are other types of testing?
23UNIT TEST INTEGRATION TEST FUNCTIONAL TEST “Isolated. Repeatable. Fast.”INTEGRATION TEST“Test interaction between units.”FUNCTIONAL TEST“Test behavior from user perspective.”
24Database Communication (ORM, ADO.NET, etc.) mock testingGoal: Test your business logicUI BehaviorsServicesBusiness CodeRepositoryDatabase Communication (ORM, ADO.NET, etc.)Database
25mocking Stunt doubles for real objects Look the same on the outside Mocking Tools:JustMock (by Telerik)Isolator (by TypeMock)MOQ (OSS)RhinoMocks (OSS)
26AAA mocking pattern//Arrange Set-up your test variables and mocks //Act Execute your code like normal //Assert Verify what happened
27DEMO: MOCKING DATABASE Testing L2S with Mock ObjectsDEMO: MOCKING DATABASE
28A 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
29hard parts Creating test schema/data Speed DbUnit In memory database SQL Lite, SQL CE, etc.
30rules 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
31DEMO: TESTinG with real DB Creating integration tests to talk to real databaseDEMO: TESTinG with real DB
34Links4GuysFromRolla 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