我在我的应用程序中使用EF4,我想为DAL方法制作测试用例,这些方法通常会访问数据库以获取数据。我正在使用Typemock框架进行模拟。我想模拟数据库调用,只想测试查询。 E.g:
ObjectContext.Where(u => u.code == Code)
为此,我需要为EF模型制作假ObjectContext
,并希望在假ObjectContext
中填充一些假数据,以便我们可以在假ObjectContext
上执行查询(LINQ) }。请建议我如何创建虚假对象上下文(使用TypeMock框架)并填充实体中的数据。
例如,我有以下方法:
protected IObjectSet<T> CreateObjectSet<T>() where T : EntityBase
{
return _context.CreateObjectSet<T>();
}
我正在创建一个模拟_context的测试用例,但_context为null。我的测试用例是:
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());
答案 0 :(得分:3)
此前已被问过几次。做了一些搜索并想出了以下资源:
使用TypeMock Isolator引入实体框架单元测试
http://mosesofegypt.net/post/Introducing-Entity-Framework-Unit-Testing-with-TypeMock-Isolator.aspx
使用存储库模式
抽象ObjectContextEF4 - possible to mock ObjectContext for unit testing?
如何在Entity Framework中模拟ObjectContext或ObjectQuery?
How to mock ObjectContext or ObjectQuery<T> in Entity Framework?