1

I have just added GraphDiff in an existing Entity Framework solution which is utilizing Moq framework for testing. All my tests that are using Moq in insert and update methods are now failing since method _context.UpdateGraph throws following exception: System.NullReferenceException: Object reference not set to an instance of an object. GraphDiff on GitHub https://github.com/refactorthis/GraphDiff

UpdateGraph extension method: https://github.com/refactorthis/GraphDiff/blob/develop/GraphDiff/GraphDiff/DbContextExtensions.cs

How should you hookup Moq with GraphDiff?

3
  • 2
    You should include more information about the inner exceptions, or perhaps use the sorce code of GrpahDiff instead of the assembly to guess where the error comes from Commented May 15, 2015 at 11:29
  • Edited the answer with more info about GraphDif fand UpdateGraph extension method Commented May 15, 2015 at 12:18
  • 1
    Mocking entityframework is a pain. You need to mock ObjectContext as well as DbContext. GraphDiff uses ObjectContext. I tried but ended up using nuget Effort instead effort.codeplex.com Commented Jun 25, 2015 at 15:02

1 Answer 1

1

We had this problem as well. This is how we solved it.

So this is in the IContext interface:

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

This is in the base context:

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

and

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

And this is in the actual concrete context:

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

and

private ObjectContext ObjectContext
        {
            get { return (this as IObjectContextAdapter).ObjectContext; }
        }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.