如何包括关联实体 [英] How to Include associated entities

查看:150
本文介绍了如何包括关联实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为下面的方法GetByEmail创建测试用例。

I want to create test case for below method "GetByEmail".

 public User GetByEmail(string email, bool includeUserRoles = false, bool includeUserType = false)
 { 
    Expression<Func<User>> whereClause = u => u.Email == email; 

    return GetQuery(whereClause, includeUserRoles, includeUserType) .FirstOrDefault(); 
 } 

 private IQueryable<User> GetQuery(Expression<Func<User>> whereClause, 
                                   bool includeUserRoles = false, bool includeUserType = false) 
 { 
   IQueryable<User> query = base.GetQuery(whereClause); 

   if (includeUserRoles) 
     query = query.Include(u => u.UserRoles); 

   if (includeUserType) 
     query = query.Include(u => u.UserType); 

   return query; 
 } 

 protected IQueryable<T> GetQuery<T>(Expression<Func<T>> predicate) where T : EntityBase
 { 
    return predicate != null ? 
             CreateObjectSet<T>().Where(predicate) : 
             CreateObjectSet<T>(); 
 } 

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

 public static IQueryable<T> Include<T>(this IQueryable<T> source, Expression<Func<T>> property)
 { 
   var objectQuery = source as ObjectQuery<T>; 

   if (objectQuery != null) 
   { 
      var propertyPath = GetPropertyPath(property); 
      return objectQuery.Include(propertyPath); 
   } 

   return source; 
 } 

以下是我的测试用例方法 -

Below is my test case method -

[Fact] 
private void GetByEmail_PassedEmailAddress_RelatedUser() 
{ 
  //Created fake context 
  var fakeContext = Isolate.Fake.Instance<Entities>(); 

  //Created fake Repository and passed fakeContext to it 
  var fakeRepository = Isolate.Fake.Instance<Repository>(Members.CallOriginal,   ConstructorWillBe.Called, fakeContext);

  //Created fake in memory collection of User 
  var fakeUsers = GetUsers(); 

  Isolate.WhenCalled(() => fakeContext.Context.CreateObjectSet<User>()) 
         .WillReturnCollectionValuesOf(fakeUsers); 

  var User = Isolate.Invoke.Method(fakeRepository, "GetByEmail", "abc@xyz.com", true, true);

  Assert.True(User != null); 
} 

在上述测试用例方法中,我成功地获得了通过电子邮件的用户,但没有能够包含其他关联用户的实体。

In the above test case method I successfully get the user with passed email but not able to include other entities of associated user.

请告诉我,如何将其他实体与关联的用户相关联。

Kindly let me know, how can I include other entities with associated User.

推荐答案

包含是漏抽象 - 它只适用于EF和linq到实体,并且不能与linq对对象成功使用。您知道您的单元测试需要填充关系,因此您的 GetUsers 方法必须准备该数据。这是一个嘲笑/假冒的点 - 你不要考虑嘲笑的方法的内部实现。你只需返回应该返回的内容。

Include is leaky abstraction - it works only with EF and linq-to-entities and cannot be successfully used with linq-to-objects. You know that your unit test needs populated relations so your GetUsers method must prepare that data. That is a point of mocking / faking - you don't think about internal implementation of mocked method. You simply return what should be returned.

Btw。你的测试点是甚么?看起来你正在试图测试一个模拟 - 这是错误的。 Mock提供正确的数据,您只需要它来测试另一个依赖于嘲笑组件的功能。

Btw. what is the point of your test? It looks like you are trying to test a mock - that is wrong. Mock provides correct data and you only need it to test another feature dependent on mocked component.

这篇关于如何包括关联实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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