测试:如何使用TypeMock为EF4模型创建假对象上下文 [英] Testing : How to create fake object context using TypeMock for EF4 model

查看:130
本文介绍了测试:如何使用TypeMock为EF4模型创建假对象上下文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序中使用EF4,而我想为DAL方法做测试用例,通常用数据库获取数据。我正在使用Typemock框架进行Mocking。我想模拟数据库调用,只想测试查询。例如:

  ObjectContext.Where(u => u.code ==代码)

为了做到这一点,我需要为EF模型制作Fake ObjectContext ,并要填写在Fake ObjectContext 中的一些假数据,以便我们可以在假的 ObjectContext 上执行我们的查询(LINQ)。请教我如何创建假对象上下文(使用TypeMock框架)并填写实体中的数据。



例如,我有以下方法:

  protected IObjectSet< ; T> CreateObjectSet< T>()其中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());


解决方案

做了一些搜索,并提出以下资源:



使用TypeMock隔离器介绍实体框架单元测试



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



通过使用ObjectContext抽象存储库模式



EF4 - 可能模拟ObjectContext进行单元测试?



如何在实体框架中模拟ObjectContext或ObjectQuery? strong>



如何模拟ObjectContext或ObjectQuery< T>在实体框架?


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());

解决方案

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?

这篇关于测试:如何使用TypeMock为EF4模型创建假对象上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆