I'm getting the following error when trying to fake a db set as a list using Moq:
System.ArgumentException HResult=0x80070057 Message=Invalid callback. Setup on method with 0 parameter(s) cannot invoke callback with different number of parameters (1). Source=Moq StackTrace:
at Moq.MethodCallReturn 2.ValidateReturnDelegate(Delegate callback)
at Moq.MethodCallReturn 2.Returns(Func`1 valueExpression) at
Here's my code. It should be relatively simple.
var mockContext = new Mock<IContext>();
var lhList = new List<LogicHistory>();
mockContext.Setup(x => x.Set<LogicHistory>()).Returns(lhList.AsDbSet);
Here's my extension method.
public static DbSet<T> AsDbSet<T>(this List<T> sourceList) where T : class
{
var queryable = sourceList.AsQueryable();
var mockDbSet = new Mock<DbSet<T>>();
mockDbSet.As<IQueryable<T>>().Setup(m => m.Provider).Returns(queryable.Provider);
mockDbSet.As<IQueryable<T>>().Setup(m => m.Expression).Returns(queryable.Expression);
mockDbSet.As<IQueryable<T>>().Setup(m => m.ElementType).Returns(queryable.ElementType);
mockDbSet.As<IQueryable<T>>().Setup(m => m.GetEnumerator()).Returns(queryable.GetEnumerator());
mockDbSet.Setup(x => x.Add(It.IsAny<T>())).Callback<T>(sourceList.Add);
mockDbSet.Setup(x => x.Remove(It.IsAny<T>())).Returns<T>(x => { if (sourceList.Remove(x)) return x; else return null; });
return mockDbSet.Object;
}
I used this exact same method a year ago and it worked perfectly. I'm looking at my old unit tests and they run fine. Then I try running this, and it doesn't work. It looks functionally identical to me. The only differences are the variable names. The Set method is identical in both context interfaces:
DbSet<TEntity> Set<TEntity>() where TEntity : class;
Anyone have any thoughts on what the problem is?
User contributions licensed under CC BY-SA 3.0