【问题标题】:Using Moq with EntityFramework graphdiff将 Moq 与 EntityFramework graphdiff 一起使用
【发布时间】:2015-05-15 11:03:34
【问题描述】:

我刚刚在使用 Moq 框架进行测试的现有实体框架解决方案中添加了 GraphDiff。 我在插入和更新方法中使用 Moq 的所有测试现在都失败了,因为方法 _context.UpdateGraph 引发以下异常:System.NullReferenceException:对象引用未设置为对象的实例。 GitHub 上的 GraphDiff https://github.com/refactorthis/GraphDiff

UpdateGraph 扩展方法: https://github.com/refactorthis/GraphDiff/blob/develop/GraphDiff/GraphDiff/DbContextExtensions.cs

您应该如何将 Moq 与 GraphDiff 连接起来?

【问题讨论】:

  • 你应该包含更多关于内部异常的信息,或者可能使用 GrpahDiff 的源代码而不是程序集来猜测错误来自哪里
  • 用关于 GraphDif 和 UpdateGraph 扩展方法的更多信息编辑了答案
  • 模拟实体框架很痛苦。您需要模拟 ObjectContext 以及 DbContext。 GraphDiff 使用 ObjectContext。我尝试过,但最终改用了 nuget Effort effort.codeplex.com

标签: entity-framework moq graphdiff


【解决方案1】:

我们也遇到过这个问题。我们就是这样解决的。

所以这是在 IContext 接口中:

T UpdateGraph<T>(T entity, Expression<Func<IUpdateConfiguration<T>, object>> mapping = null) where T : class, new();

DbEntityEntry<TEntity> Entry<TEntity>(TEntity entity) where TEntity : class;
        DbEntityEntry Entry(object entity);

        DbContextConfiguration Configuration { get; }

这是在基本上下文中:

 public virtual T UpdateGraph<T>(T entity, Expression<Func<IUpdateConfiguration<T>, object>> mapping = null) where T : class, new()
        {
            return null;
        }

private ObjectContext ObjectContext
        {
            get { return (this as IObjectContextAdapter).ObjectContext; }
        }

这是在实际的具体上下文中:

public override T UpdateGraph<T>(T entity, Expression<Func<IUpdateConfiguration<T>, object>> mapping = null) // where T : class, new()
        {
            return DbContextExtensions.UpdateGraph<T>(this, entity, mapping);
        }

private ObjectContext ObjectContext
        {
            get { return (this as IObjectContextAdapter).ObjectContext; }
        }

【讨论】:

    猜你喜欢
    • 2014-11-03
    • 1970-01-01
    • 1970-01-01
    • 2016-05-15
    • 1970-01-01
    • 1970-01-01
    • 2016-06-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多