2015-05-15 66 views
1

我剛剛在一個現有的使用Moq框架進行測試的實體框架解決方案中添加了GraphDiff。 我所有在插入和更新方法中使用Moq的測試都失敗了,因爲方法_context.UpdateGraph拋出以下異常:System.NullReferenceException:未將對象引用設置爲對象的實例。 GraphDiff在GitHub上 https://github.com/refactorthis/GraphDiff將Moq與EntityFramework結合使用graphdiff

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

你應該如何聯播起訂量與GraphDiff?

+2

您應該包括關於內部異常的詳細信息,或者使用GrpahDiff的SORCE代碼,而不是組裝的猜測,其中的誤差來自 – JotaBe

+0

編輯答案與更多的信息關於GraphDif FAND UpdateGraph擴展方法 – vipasane

+1

嘲諷實體框架是一種痛苦。你需要模擬ObjectContext以及DbContext。 GraphDiff使用ObjectContext。我嘗試過,但最終使用nuget Effort而不是https://effort.codeplex.com/ – mortb

回答

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; } 
     }