我正在嘗試使用FakeDbSet爲單元測試製作FakeDbContext。IDbAsyncEnumerable not implemented
但我得到以下錯誤(見下文)。我正在擴展DbSet,因此通常應該實現IDbAsyncEnumerable。當我實施它時,它說它沒有用處。
例外:
System.InvalidOperationException:源的IQueryable不 實現 IDbAsyncEnumerable。實現IDbAsyncEnumerable的源只有 可用於實體 框架異步操作。詳情請參閱 http://go.microsoft.com/fwlink/?LinkId=287068。
FakeDbSet類:
public abstract class FakeDbSet<TEntity> : DbSet<TEntity>, IEnumerable<TEntity>, IQueryable, IDbAsyncEnumerable<TEntity> where TEntity : Entity, new()
{
#region Private Fields
private readonly ObservableCollection<TEntity> _items;
private readonly IQueryable _query;
#endregion Private Fields
protected FakeDbSet()
{
_items = new ObservableCollection<TEntity>();
_query = _items.AsQueryable();
}
public Expression Expression { get { return _query.Expression; } }
public Type ElementType { get { return _query.ElementType; } }
public IQueryProvider Provider { get { return _query.Provider; } }
public override TEntity Add(TEntity entity)
{
_items.Add(entity);
return entity;
}
public override TEntity Remove(TEntity entity)
{
_items.Remove(entity);
return entity;
}
public override TEntity Attach(TEntity entity)
{
switch (entity.ObjectState)
{
case ObjectState.Modified:
_items.Remove(entity);
_items.Add(entity);
break;
case ObjectState.Deleted:
_items.Remove(entity);
break;
case ObjectState.Unchanged:
case ObjectState.Added:
_items.Add(entity);
break;
default:
throw new ArgumentOutOfRangeException();
}
return entity;
}
public override TEntity Create() { return new TEntity(); }
public override TDerivedEntity Create<TDerivedEntity>() { return Activator.CreateInstance<TDerivedEntity>(); }
public override ObservableCollection<TEntity> Local { get { return _items; } }
IEnumerator<TEntity> IEnumerable<TEntity>.GetEnumerator()
{
return _items.GetEnumerator();
}
Type IQueryable.ElementType
{
get { return _items.AsQueryable().ElementType; }
}
Expression IQueryable.Expression
{
get { return _items.AsQueryable().Expression; }
}
IQueryProvider IQueryable.Provider
{
get { return _items.AsQueryable().Provider; }
}
這裏是代碼中的要點。在要點的最後一個文件中,這是錯誤發生的地方。 Gist code
我確實添加了所給的類,但我不知道如何將它與我的測試連接起來。我目前不使用moq。我將把所有的代碼添加到問題中。 @Hans帕桑特 – kevingoos 2014-10-13 07:29:47
Passent:https://gist.github.com/ghost606/36718ff9670aad9c4f2e#file-searchdoubtfuls-cs – kevingoos 2014-10-13 07:52:14