C# 为单元测试创建存储库

C# 为单元测试创建存储库,c#,linq,entity-framework,unit-testing,repository-pattern,C#,Linq,Entity Framework,Unit Testing,Repository Pattern,我已经用C#、MVC、实体框架、LINQ等编写了一个web应用程序。。。现在我想为整个项目追溯创建单元测试 我知道,为了编写有效的单元测试,我需要为模型创建存储库,以便对它们进行模拟,因此实体框架将对数据库进行任何点击 我有3个主要型号;类别,密码,用户密码 类别包含类别和密码。和密码包含用户密码(链接到用户帐户)。所以我用基本方法创建了3个存储库,比如;创建、删除、更新等 但是,看看这个LINQ查询: var selectedCategoryItem = DatabaseContext.Cat

我已经用C#、MVC、实体框架、LINQ等编写了一个web应用程序。。。现在我想为整个项目追溯创建单元测试

我知道,为了编写有效的单元测试,我需要为模型创建存储库,以便对它们进行模拟,因此实体框架将对数据库进行任何点击

我有3个主要型号;类别,密码,用户密码

类别包含类别和密码。和密码包含用户密码(链接到用户帐户)。所以我用基本方法创建了3个存储库,比如;创建、删除、更新等

但是,看看这个LINQ查询:

var selectedCategoryItem = DatabaseContext.Categories
                .Where(c => c.CategoryId == ParentCategoryId)
                .Include(c => c.SubCategories)
                .Include(c => c.Passwords)
                .Include(c => c.Passwords.Select(p => p.Creator))
                .ToList()
                .Select(c => new Category()
                {

                SubCategories = c.SubCategories
                            .Where(sub => !sub.Deleted)
                            .OrderBy(sub => sub.CategoryOrder)
                            .ToList(),                          //make sure only undeleted subcategories are returned

                Passwords = c.Passwords
                            .Where(pass => !pass.Deleted
                            && (
                            (UserPasswordList.Any(up => up.PasswordId == pass.PasswordId && up.Id == UserId))
                            || (userIsAdmin && ApplicationSettings.Default.AdminsHaveAccessToAllPasswords)
                            || pass.Creator_Id == UserId
                            )
                            )   //make sure only undeleted passwords - that the current user has acccess to - are returned
                            .OrderBy(pass => pass.PasswordOrder)
                            .ToList(),

                CategoryId = c.CategoryId,
                CategoryName = c.CategoryName,
                Category_ParentID = c.Category_ParentID,
                CategoryOrder = c.CategoryOrder,
                Parent_Category = c.Parent_Category,
                Deleted = c.Deleted
                })
                .SingleOrDefault();
如果存储库只有保存、更新、删除、插入、FindById等基本方法。。。。我应该如何将查询分解到存储库中?还有很多业务逻辑,比如过滤用户可以访问的密码,应该放在哪里


我读了一些关于返回IQueryable对象的内容,以便可以在服务层中更改LINQ查询。。。这看起来怎么样?

如果您想编写单元测试,所有LINQ到实体都应该在接口后面

e、 g

或者,由于用户可能在整个站点上,您可以传递

public interface IUserConfig {
     int PasswordId{ get; set; }
     int UserId { get; set;}
}


// Constructor for CategoryRepository : ICategoryRepository 
public CategoryRepository(DbContext context, IUserConfig)  
{
   ....
}
然后

public List GetCategoriesForParentId(int-id){
return\u context.Categories
.....
密码=c.Passwords.Where(
.....
(UserPasswordList.Any(up=>up.PasswordId==\u userConfig.PasswordId
&&up.Id==\u userConfig.UserId))
.....
}

编辑2:刚刚注意到
.SingleOrDefault()
,而不是返回
列表
您将返回
类别

我无法理解的是,让我们使用List GetCategoriesForParentId-这将返回一个类别列表,但我需要返回一个类别和密码列表…那么如何使用存储库模式实现这一点呢h2调用sso,类似于:var root=new Category(){subcategories=GetCategoriesForParentId(1),password=GetPasswordsForParentId(1)};或者您也可以将UserDetails传递到该方法中,或者将某种IConfig/IUserConfig传递给存储库构造函数UserDetails或IConfig实际包含哪些内容?您看过了吗?
List<Category> GetCategoriesForParentIdAnduser(int id, int passwordId, int userId);
public interface IUserConfig {
     int PasswordId{ get; set; }
     int UserId { get; set;}
}


// Constructor for CategoryRepository : ICategoryRepository 
public CategoryRepository(DbContext context, IUserConfig)  
{
   ....
}
public List<Category> GetCategoriesForParentId(int id){
     return _context.Categories
                .....
                Passwords = c.Passwords.Where(
                            .....
                            (UserPasswordList.Any(up => up.PasswordId == _userConfig.PasswordId 
                                                     && up.Id == _userConfig.UserId))                            
                .....
}