With Mocha and Karma Telerik Academy Telerik Software Academy.
Building a Testable Data Access Layer Todd Anglin Chief Evangelist, Telerik Level: Intermediate.
-
Upload
richard-crowley -
Category
Documents
-
view
221 -
download
5
Transcript of Building a Testable Data Access Layer Todd Anglin Chief Evangelist, Telerik Level: Intermediate.
Building a Testable Data Building a Testable Data Access LayerAccess LayerTodd AnglinTodd AnglinChief Evangelist, Telerik
Level: Intermediate
IntroductionsIntroductions
Todd Anglin
Chief Evangelist, TelerikMicrosoft MVP
ASP InsiderPresident NHDNUG & O’Reilly Author
@toddanglin
TelerikWatch.com
today’s plantoday’s plan
what is a “data layer”?what is a “data layer”?
PresentationPresentation
Data LayerData Layer
Responsible for talking to “persistence layer”
Web Desktop
Domain Logic(“the code that makes you money”)
Domain Logic(“the code that makes you money”)
Data / ModelData / Model
PersistencePersistence
Database Cloud XML Etc.
ServiceService
why build a data layer?why build a data layer?what’s the benefit?
importance of data layerimportance of data layer
• Decouple application from persistence= easier maintenance
= improved testability
= greater reusability
PresentationPresentation
Web Desktop
Domain Logic(“the code that makes you money”)
Domain Logic(“the code that makes you money”)
DataData
PersistencePersistence
Database Cloud XML Etc.
ServiceService
a a goodgood data layer… data layer…
1. Handles all data access
2. Hides implementation
3. Flexible– Easy to refactor
a a badbad data layer is… data layer is…
1. Does not centralize data access
2. Makes application very dependent on persistent store
3. Easy*
how do we build “data layers”?how do we build “data layers”?
By Hand
Pros-POCO-YAGNI-No-RTFMCons-Time-No FM
By Hand
Pros-POCO-YAGNI-No-RTFMCons-Time-No FM
ORM
Pros-Time-FlexibleCons-Learning-Limits-Trust*
ORM
Pros-Time-FlexibleCons-Learning-Limits-Trust*
popular .NET ORMspopular .NET ORMs
DEMODEMOBuild data layer with LinqToSql & EF & OpenAccess
data layer patternsdata layer patterns
• Domain Driven Design (DDD)– Key concepts: Repositories act on model
• ActiveRecord– Key concepts: Model objects act on themselves
• Data Mapper– Key concepts: Objects mapped to tables
DEMODEMOAdd data access pattern to project
testing the data layer
two testing campstwo testing camps
A Test Database B Mock
Database
Concepts:•Test against “real” database•Use set-up/tear-down code to create test data
Good When…You put lots of logic in your database
Concepts:•Test against “fake” database•Isolates your code from database behavior
Good When…You want fast unit tests and you put most logic in code
Mocking
Pros-Unit test-Isolate concerns-FastCons-“Hides” issues-Does not test database logic
Mocking
Pros-Unit test-Isolate concerns-FastCons-“Hides” issues-Does not test database logic
“Test” Database
Pros-Catches more issues-FamiliarCons-Slow-Not a “unit” test
“Test” Database
Pros-Catches more issues-FamiliarCons-Slow-Not a “unit” test
picking a camppicking a camp
UNIT TEST“Isolated. Repeatable. Fast.”
INTEGRATION TEST“Test interaction between units.”
FUNCTIONAL TEST“Test behavior from user perspective.”
mock testingmock testing
Goal: Test your business logic
B
Database Communication(ORM, ADO.NET, etc.)
Database Communication(ORM, ADO.NET, etc.)
RepositoryRepository
Business CodeBusiness Code
UI BehaviorsUI Behaviors ServicesServices
DatabaseDatabase
mockingmocking
• Stunt doubles for real objects– Look the same on the outside
Mocking Tools:JustMock (by Telerik)Isolator (by TypeMock)
MOQ (OSS)RhinoMocks (OSS)
AAA mocking patternAAA mocking pattern
//Arrange
Set-up your test variables and mocks
//Act
Execute your code like normal
//Assert
Verify what happened
DEMO: MOCKING DEMO: MOCKING DATABASEDATABASE
Testing L2S with Mock Objects
test databasetest database
Goal: Test your business logic + database behavior
A
Steps for every test:1.Create database schema + test data2. (Optional) Test database setup
correctly3.Execute unit test code4.Verify database behaved correctly
hard partshard parts
1. Creating test schema/data– DbUnit
2. Speed– In memory database
– SQL Lite, SQL CE, etc.
rules for test database testsrules for test database tests
1. Prior to running tests, schema should be redeployed to test DB (+ test data)
2. Tests should not change existing data– Edits, Deletes should be on records created by test
– Original data should be read-only
3. Tests should not depend on changes from previous tests
DEMO: TESTING WITH DEMO: TESTING WITH REAL DBREAL DB
Creating integration tests to talk to real database
should you test your DAL?should you test your DAL?
LinksLinks• 4GuysFromRolla on Testing DAL (2005)
– http://aspnet.4guysfromrolla.com/articles/040605-1.2.aspx
• Unit Testing the DAL (Java, but great discussion of DAL data testing)– http://www.buunguyen.net/blog/unit-testing-the-data-access-layer.html
• Roy Osherove on using Mocks for DAL testing– http://weblogs.asp.net/rosherove/archive/2003/09/30/29734.aspx
• SQL Lite project page– http://www.sqlite.org/docs.html– System.Data.SQLite: http://sqlite.phxsoftware.com/– http://www.mikeduncan.com/sqlite-on-dotnet-in-3-mins/– http://hendryluk.wordpress.com/2008/10/07/data-access-test-with-sqlite/
• DbUnit.NET (last updated 2006 – still alpha)– http://dbunit-net.sourceforge.net/GettingStarted.html
• SQL Server Compact 4 CTP1 (2010)– http://www.microsoft.com/downloads/details.aspx?FamilyID=0d2357ea-324f-46fd-88fc-7364c80e4fdb&displaylang=en– http://robtiffany.com/sql-server-compact/here-comes-sql-server-compact-4-0
• SQL Script to clear all tables in database– http://www.smallworkarounds.net/2009/02/script-to-delete-all-data-from-sql.html
• Microsoft.SqlServer.Management.Smo primer– http://davidhayden.com/blog/dave/archive/2006/01/27/2774.aspx– http://social.msdn.microsoft.com/Forums/en-US/adodotnetdataproviders/thread/43e8bc3a-1132-453b-b950-09427e970f31
• Multi-tier L2S architecture ideas– http://blog.stevensanderson.com/2007/11/29/linq-to-sql-the-multi-tier-story/