C# 最小起订量-设置在哪里扩展?
如何在对象上设置where linq扩展<代码>数据库集在我的例子中。这是我的密码:C# 最小起订量-设置在哪里扩展?,c#,.net,linq,unit-testing,moq,C#,.net,Linq,Unit Testing,Moq,如何在对象上设置where linq扩展数据库集在我的例子中。这是我的密码: this.workflowStateSet .Setup(m => m.Where(It.IsAny<Expression<Func<Model.WorkflowState, int, bool>>>())) .Returns(new List<Model.WorkflowState>().AsQueryable()); this.workflowSt
this.workflowStateSet
.Setup(m => m.Where(It.IsAny<Expression<Func<Model.WorkflowState, int, bool>>>()))
.Returns(new List<Model.WorkflowState>().AsQueryable());
this.workflowStateSet
.Setup(m=>m.Where(It.IsAny()))
.Returns(new List().AsQueryable());
但是,它给了我一个不太熟悉的异常:
System.NotSupportedException:表达式引用的方法
不属于模拟对象:m=>m.Where
如果有任何提示,我将不胜感激。使用存储库模式为数据检索添加一层抽象。这种抽象可以被模仿 例如,如果您试图检索stateId等于1的所有工作流,则不调用类似的调用
var result = DbSet.WorkflowState.Where(w => w.stateId == 1);
将此代码移到另一个类中,然后创建方法签名的接口
public interface IWorkflowStateSetRepository{
IQueryable<Model.WorkflowState> GetAllWorkflows(int state);
}
公共接口IWorkflowStateSetRepository{
IQueryable GetAllWorkflows(int状态);
}
实施
public class WorkflowStateSetRepository : IWorkflowStateSetRepository{
public IQueryable<Model.WorkflowState> GetAllWorkflows(int state){
return DbSet.WorkflowState .Where(w => w.stateId == state);
}
}
公共类WorkflowStateSetRepository:IWorkflowStateSetRepository{
公共IQueryable GetAllWorkflows(int状态){
返回DbSet.WorkflowState.Where(w=>w.stateId==state);
}
}
在调用代码中,获取IWorkflowStateSetRepository的实例(可能来自IoC容器),并调用GetAllWorkflows()方法。这将提供与以前相同的结果,但现在可以在测试和对该方法的设置调用中模拟接口
this.MockedIWorkflowStateSetRepository.Setup(m => m.GetAllWorkflows(It.IsAny<int>()))
.Returns(new List<Model.WorkflowState>().AsQueryable());
this.MockedIWorkflowStateSetRepository.Setup(m=>m.GetAllWorkflows(It.IsAny()))
.Returns(new List().AsQueryable());
这段代码更易于维护,并且(使用适当命名的变量和方法)也能更好地传达意图
这里将更详细地讨论存储库模式
使用存储库模式为数据检索添加抽象层。这种抽象可以被模仿 例如,如果您试图检索stateId等于1的所有工作流,则不调用类似的调用
var result = DbSet.WorkflowState.Where(w => w.stateId == 1);
将此代码移到另一个类中,然后创建方法签名的接口
public interface IWorkflowStateSetRepository{
IQueryable<Model.WorkflowState> GetAllWorkflows(int state);
}
公共接口IWorkflowStateSetRepository{
IQueryable GetAllWorkflows(int状态);
}
实施
public class WorkflowStateSetRepository : IWorkflowStateSetRepository{
public IQueryable<Model.WorkflowState> GetAllWorkflows(int state){
return DbSet.WorkflowState .Where(w => w.stateId == state);
}
}
公共类WorkflowStateSetRepository:IWorkflowStateSetRepository{
公共IQueryable GetAllWorkflows(int状态){
返回DbSet.WorkflowState.Where(w=>w.stateId==state);
}
}
在调用代码中,获取IWorkflowStateSetRepository的实例(可能来自IoC容器),并调用GetAllWorkflows()方法。这将提供与以前相同的结果,但现在可以在测试和对该方法的设置调用中模拟接口
this.MockedIWorkflowStateSetRepository.Setup(m => m.GetAllWorkflows(It.IsAny<int>()))
.Returns(new List<Model.WorkflowState>().AsQueryable());
this.MockedIWorkflowStateSetRepository.Setup(m=>m.GetAllWorkflows(It.IsAny()))
.Returns(new List().AsQueryable());
这段代码更易于维护,并且(使用适当命名的变量和方法)也能更好地传达意图
这里将更详细地讨论存储库模式
您是否正在尝试模拟真实的DbSet实例?因为这不起作用,因为错误消息试图解释您。要模拟类型,它必须是接口或具有虚拟成员(抽象成员也是虚拟的) 您可以尝试模拟IDbSet或创建一个自定义DbSet类,例如下面的类
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
public class DbSetMock<T> : DbSet<T>, IDbSet<T>
where T : class
{
private readonly ICollection<T> _contentCollection;
public DbSetMock(IList<T> contentCollection = null)
{
_contentCollection = new Collection<T>(contentCollection ?? new List<T>());
AddedEntities = new List<T>();
RemovedEntities = new List<T>();
AttachedEntities = new List<T>();
}
public void OverrideContentCollection(IEnumerable<T> newData)
{
_contentCollection.Clear();
_contentCollection.AddRange(newData);
}
public IList<T> AddedEntities { get; private set; }
public IList<T> AttachedEntities { get; private set; }
public override ObservableCollection<T> Local
{
get
{
throw new NotImplementedException();
}
}
public IList<T> RemovedEntities { get; private set; }
public Type ElementType
{
get
{
return typeof(T);
}
}
public Expression Expression
{
get
{
return _contentCollection.AsQueryable().Expression;
}
}
public IQueryProvider Provider
{
get
{
return _contentCollection.AsQueryable().Provider;
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public IEnumerator<T> GetEnumerator()
{
return _contentCollection.GetEnumerator();
}
public override T Add(T entity)
{
AddedEntities.Add(entity);
_contentCollection.Add(entity);
return entity;
}
public override T Attach(T entity)
{
AttachedEntities.Add(entity);
var matchingEntity = _contentCollection.SingleOrDefault(x => x.Id == entity.Id);
if (matchingEntity != null)
{
_contentCollection.Remove(matchingEntity);
}
_contentCollection.Add(entity);
return entity;
}
public override TDerivedEntity Create<TDerivedEntity>()
{
throw new NotImplementedException();
}
public override T Create()
{
throw new NotImplementedException();
}
public override T Find(params object[] keyValues)
{
throw new NotImplementedException();
}
public override T Remove(T entity)
{
RemovedEntities.Add(entity);
_contentCollection.Remove(entity);
return entity;
}
}
使用系统;
使用系统集合;
使用System.Collections.Generic;
使用System.Collections.ObjectModel;
使用System.Data.Entity;
使用System.Linq;
使用System.Linq.Expressions;
公共类DbSetMock:DbSet,IDbSet
T:在哪里上课
{
私有只读ICollection\u contentCollection;
公共DbSetMock(IList contentCollection=null)
{
_contentCollection=新集合(contentCollection??new List());
AddedEntities=新列表();
RemovedEntities=新列表();
AttachedEntities=新列表();
}
public void OverrideContentCollection(IEnumerable newData)
{
_contentCollection.Clear();
_contentCollection.AddRange(newData);
}
公共IList附加属性{get;private set;}
公共IList附件{get;private set;}
公共覆盖可观测收集本地
{
得到
{
抛出新的NotImplementedException();
}
}
公共IList RemovedEntities{get;private set;}
公共类型ElementType
{
得到
{
返回类型(T);
}
}
公开表达
{
得到
{
返回_contentCollection.AsQueryable()表达式;
}
}
公共IQueryProvider提供程序
{
得到
{
返回_contentCollection.AsQueryable().Provider;
}
}
IEnumerator IEnumerable.GetEnumerator()
{
返回GetEnumerator();
}
公共IEnumerator GetEnumerator()
{
返回_contentCollection.GetEnumerator();
}
公共覆盖T添加(T实体)
{
附录。添加(实体);
_contentCollection.Add(实体);
返回实体;
}
公共覆盖T附加(T实体)
{
附件。添加(实体);
var matchingEntity=\u contentCollection.SingleOrDefault(x=>x.Id==entity.Id);
if(匹配性!=null)
{
_contentCollection.Remove(匹配性);
}
_contentCollection.Add(实体);
返回实体;
}
公共覆盖TDerivedEntity Create()
{
抛出新的NotImplementedException();
}
公共重写T Create()
{
抛出新的NotImplementedException();
}
public override T Find(参数对象[]键值)
{
抛出新的NotImplementedException();
}
公共覆盖T删除(T实体)
{
删除实体。添加(实体);
_c