Upload
luis-gonzalez-castro
View
218
Download
0
Embed Size (px)
Citation preview
8/18/2019 4 Efarchitecture m4 Testing Slides
1/23
Entity Framework: Automated Testing
Build Automated Tests for EF-dependent code
Julie Lerman
thedatafarm.com/blog
8/18/2019 4 Efarchitecture m4 Testing Slides
2/23
Goals
Automated Testing Overview Testing Database Interaction
Testing Entity Framework Integration with your logic
Avoiding EF and the DB with Fakes
Resources
8/18/2019 4 Efarchitecture m4 Testing Slides
3/23
SystemTesting
InteractionTesting
Automated Testing
UnitTesting
User InterfaceTesting
IntegrationTesting
8/18/2019 4 Efarchitecture m4 Testing Slides
4/23
8/18/2019 4 Efarchitecture m4 Testing Slides
5/23
Testing Code that Interacts with the Database
My Business Logic
Adds some data tothe database
My Concern
Does it really work?My Test
Run the method
Query the databaseto see if the data isreally there
8/18/2019 4 Efarchitecture m4 Testing Slides
6/23
But
is it really
a unit test?
8/18/2019 4 Efarchitecture m4 Testing Slides
7/23
Unit Testing when EF Intrudes
public List OrdersFromAPromotion(Promotion promo)
{
if (promo.StartDate o.PromotionId == promo.PromotionId)
.ToList();
}
return null;}
8/18/2019 4 Efarchitecture m4 Testing Slides
8/23
Testing for Both Responses
[TestMethod]
public void GetOrdersForPastPromotionsDoesNotSkipQuery()
[TestMethod]public void
GetOrdersForFuturePromotionsSkipsQueryAndReturnsNull()
8/18/2019 4 Efarchitecture m4 Testing Slides
9/23
Unit Testing when EF Intrudes
public List OrdersViaPromotion(Promotion promo)
{
if (promo.StartDate o.PromotionId == promo.PromotionId)
.ToList();
}
return null;}
8/18/2019 4 Efarchitecture m4 Testing Slides
10/23
8/18/2019 4 Efarchitecture m4 Testing Slides
11/23
Unit Testing when EF Intrudes
public List OrdersViaPromotion(Promotion promo)
{
if (promo.StartDate o.PromotionId == promo.PromotionId)
.ToList();
}
return null;}
EF Context& Database
Fake Context& Fake Data
8/18/2019 4 Efarchitecture m4 Testing Slides
12/23
MyCustom FakeDbSet
: IDbSet
Step 1: Abstracting DbSet
System.Data.Entity.
IDbSet
System.Data.Entity.DbSet
8/18/2019 4 Efarchitecture m4 Testing Slides
13/23
Adding in a Fake IDbSet Implementation
1. Create abstract FakeDbSet : IDbSet Note abstract Find method
2. Derive FakeDbSet
PromotionFakeDbSet
3. Prep existing context classes*
SalesContext
8/18/2019 4 Efarchitecture m4 Testing Slides
14/23
MyCustom FakeObjectSet
: IObjectSet
Step 1: Abstracting ObjectSet
System.Data.Entity.
IObjectSet
System.Data.Entity.ObjectSet
8/18/2019 4 Efarchitecture m4 Testing Slides
15/23
Step 2: …and then Abstracting the Context
FakeSalesContext
:ISalesContext
My custom
ISalesContext
SalesContext:DbContext
:ISalesContext
UOW/RepositorytakesISalesContext IUow
needsIContext
IContext
(No pre-defined DbSets)
IDbSet(Customer)IDbSet(Order)
IDbSet(…)DbSet(Customer)DbSet(Order)
DbSet(…)
FakeDbSet(Customer)FakeDbSet(Order)
FakeDbSet(…)
8/18/2019 4 Efarchitecture m4 Testing Slides
16/23
IRepository
IUnitofWork
Abstractions & Their Interactions
IContext
IDbSet
8/18/2019 4 Efarchitecture m4 Testing Slides
17/23
SalesRepositoryUoW
SalesContextDbSet
DbSet < … >
SalesRepository
UoW
FakeSalesContextFakeOrderDbSet
FakexxDbSet
CustomerRepository
UoW
CustSvcContextDbSet
DbSet < … >
8/18/2019 4 Efarchitecture m4 Testing Slides
18/23
IContext
IMode lContextIDbSet(class A)
IDbSet(classB)
Mode lContextIDbSet(class A)IDbSet(classB)
FakeMode lContextIDbSet(class A)IDbSet(classB)
IUnitOfWork
UnitOfWork
System.Data.Entity.
IDbSet
abstract
FakeDbSet
Class FakeDbSet:FakeDbSet
IEntityRepository
IClass Repository:IEntityRepository
Class Repository
: IClass Repository
Layers, Layers & More Layers
8/18/2019 4 Efarchitecture m4 Testing Slides
19/23
Unit of Work
Classes
UI
Getting EF Out of the Way
Tests
System.Data.Entity
Metadata &DbContext
FakeDbSet
FakeContext B
ositoriesositoriesositoriesRepositories
ClassesClassesClasses
8/18/2019 4 Efarchitecture m4 Testing Slides
20/23
Unit Testing when EF Intrudes
public List OrdersViaPromotion(Promotion promo)
{
if (promo.StartDate o.PromotionId == promo.PromotionId)
.ToList();
}
return null;}
8/18/2019 4 Efarchitecture m4 Testing Slides
21/23
Over-Abstracting the DbContext or DbSet?
IContext???
FakeDbSet???
8/18/2019 4 Efarchitecture m4 Testing Slides
22/23
Summary
8/18/2019 4 Efarchitecture m4 Testing Slides
23/23
Resources
My books Programming Entity Framework
Programming Entity Framework: Code First
Programming Entity Framework: DbContext
My website & blogs:
thedatafarm.com (mentoring) thedatafarm.com/blog, learnentityframework.com
Entity Framework Team: blogs.msdn.com/adonet
Domain Driven Design Starting Point: www.domaindrivendesign.org
More EF, Patterns & Testing Videos:
Pluralsight On-Demand Training: pluralsight.com
MSDN Developer Center: msdn.com/data/ef
http://www.domaindrivendesign.org/http://www.domaindrivendesign.org/