3

I am using EF4 in my application and I want to make test cases for DAL methods, which generally hit the database to get data. I am using the Typemock framework for Mocking. I want to mock database call and only want to test queries. E.g.:

ObjectContext.Where(u => u.code == Code) 

For doing this, I need to make Fake ObjectContext for EF Models and want to fill some fake data in Fake ObjectContext so that we can execute our queries (LINQ) on fake ObjectContext. Kindly suggest how can I can create fake object context(Using TypeMock framework) and fill data in the entities.

For example, I have the following method:

protected IObjectSet<T> CreateObjectSet<T>() where T : EntityBase 
{ 
    return _context.CreateObjectSet<T>(); 
}

And I am creating a test case to mock _context, however _context is null. My test case is:

var fakeInMemoryBlogs = GetUsers();    
var fakeContext = Isolate.Fake.Instance<SecurityEntitiesUOW>(); 
var fakeGenericRepository = Isolate.Fake.Instance<GenericRepository>
    (Members.CallOriginal, ConstructorWillBe.Called, fakeContext); 
Isolate.WhenCalled(() => fakeContext.Context.CreateObjectSet<SecUser>()) 
    .WillReturnCollectionValuesOf(fakeInMemoryBlogs.AsQueryable());
Christophe Geers
  • 8,564
  • 3
  • 37
  • 53
Yogesh
  • 829
  • 2
  • 8
  • 21
  • 1
    The question is if it makes sense to fake it: http://stackoverflow.com/questions/6766478/unit-testing-dbcontext and http://stackoverflow.com/questions/6904139/fake-dbcontext-of-entity-framework-4-1-to-test/6904479#6904479 – Ladislav Mrnka Aug 17 '11 at 07:46
  • Good point on the LINQ-to-Entities Vs LINQ-to-Objects difference when mocking the context, Ladislav. Maybe encapsulate the LINQ queries and test those against a real database and mock those queries when testing the business logic. – Christophe Geers Aug 17 '11 at 08:41
  • Hi, I want to create Fake ObjectContext for unittesting using TypeMock and fill all required entities in fake ObjectContext, so that I can verify lamda expressions and include clause only without hitting database. Kindly suggest me. Thanks – Yogesh Aug 17 '11 at 11:32

1 Answers1

3

This has been asked a couple of times before. Did some searching and came up with the following resources:

Introducing Entity Framework Unit Testing with TypeMock Isolator

http://mosesofegypt.net/post/Introducing-Entity-Framework-Unit-Testing-with-TypeMock-Isolator.aspx

Abstract the ObjectContext by using the Repository Pattern

EF4 - possible to mock ObjectContext for unit testing?

How to mock ObjectContext or ObjectQuery in Entity Framework?

How to mock ObjectContext or ObjectQuery<T> in Entity Framework?

Community
  • 1
  • 1
Christophe Geers
  • 8,564
  • 3
  • 37
  • 53
  • Thanks, but I have a method something like : protected IObjectSet CreateObjectSet() where T : EntityBase { return _context.CreateObjectSet(); }, and I am creating test case to mock _context with fake ObjectContext, It is showing _context is null. – Yogesh Aug 17 '11 at 08:43
  • My test case something e.g: var fakeInMemoryBlogs = GetUsers(); //(2) create fake context var fakeContext = Isolate.Fake.Instance(); var fakeGenericRepository = Isolate.Fake.Instance(Members.CallOriginal, ConstructorWillBe.Called, fakeContext); //(3) Setup expected call to MyBlogContext.Blogs property through the fake context Isolate.WhenCalled(() => fakeContext.Context.CreateObjectSet()) .WillReturnCollectionValuesOf(fakeInMemoryBlogs.AsQueryable()); – Yogesh Aug 17 '11 at 08:49
  • Hi, I want to create Fake ObjectContext for unittesting using TypeMock and fill all required entities in fake ObjectContext, so that I can verify lamda expressions and include clause only without hitting database. Kindly suggest me. Thanks – Yogesh Aug 17 '11 at 11:33