c# - 如何伪造 EntityFramework.Extended FutureQuery?

标签 c# entity-framework testing entity-framework-6 entity-framework-extended

我正在寻找一个虚假的存储库。

public class FooRepo {

    public FutureFoo<Foo> GetById(int id) {

        var foo = new Foo();
        return new FutureValue(foo);
    }

    public FutureQuery<Foo> GetByCategory(int categoryId) {

        var foos = new[] { new Foo(), new Foo() new Foo() };

        return  //what goes here?
    }

}

这样做的目的是编写数据相关测试而不依赖于任何数据库连接。这对于 FutureValue<> 来说真的很简单类型,因为它提供了一个接受直接对象的构造函数。但是 FutureQuery<> 的构造函数接受参数 IQueryable query, Action loadAction

我可以忽略loadAction吗? ?

如:new FutureQuery<Foo>(foos.AsQueryable(), () => { });

或者什么是正确的处理方式?


强制解决方案:

(FutureQuery<Foo>) Activator.CreateInstance(typeof(FutureQuery<Foo>),
                   BindingFlags.NonPublic | BindingFlags.Instance, null, 
                   new object[] { foos.AsQueryable(), null }, null);

最佳答案

取自FutureQueryBase.GetResult() :

    /// <summary>
    /// Gets the result by invoking the <see cref="LoadAction"/> if not already loaded.
    /// </summary>
    /// <returns>
    /// An <see cref="T:System.Collections.Generic.IEnumerable`1"/> that can be used to iterate through the collection.
    /// </returns>
    protected virtual IEnumerable<T> GetResult()
    {
        if (IsLoaded)
            return _result;

        // no load action, run query directly
        if (LoadAction == null)
        {
            _isLoaded = true;
            _result = _query as IEnumerable<T>;
            return _result;
        }

        // invoke the load action on the datacontext
        // result will be set with a callback to SetResult
        LoadAction.Invoke();
        return _result ?? Enumerable.Empty<T>();
    }

您应该为加载操作传递一个null,除非您想通过SetResult(ObjectContext, DbDataReader) 显式更新_result .

关于c# - 如何伪造 EntityFramework.Extended FutureQuery?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37149114/

相关文章:

c# - 如何为 C# 添加数据库文件的相对路径

javascript - 在 Angular 中将对象数组传递给 POST

c# - 使用 Entity Framework C# 执行通用 SQL 查询

testing - 如何访问同一测试套件中另一个测试用例的测试步骤请求

testing - 用于测试的假 EC2 端点

c# - 在多个类别中使用一个 Random 对象是否会降低每个类别的随机性?

c# - dllimport : c code affect a struct passed by reference (c#)

c# - 如何使用 EF Core Power Tools 在同一个项目中管理多个 dbcontext?

c# - Linq to Entities 中的联合订单

ios - 如何使用 .p8 JWT key 测试推送? (APNs 授权 key )