I have a Moq DbSet that has been working until recently, however since the last update of dependencies it keeps throwing a NotImplementedException
on IQueryable.Provider
Code used as follows:
var mockSet = new Mock<DbSet<A>>();
var list = new List<A>();
var queryable = list.AsQueryable();
mockSet.As<IQueryable<A>>().Setup(m => m.Provider).Returns(queryable.Provider);
mockSet.As<IQueryable<A>>().Setup(m => m.Expression).Returns(queryable.Expression);
mockSet.As<IQueryable<A>>().Setup(m => m.ElementType).Returns(queryable.ElementType);
mockSet.As<IQueryable<A>>().Setup(m => m.GetEnumerator()).Returns(() => queryable.GetEnumerator());
var f =mockSet.Object.FirstOrDefault(); // NotImplementedException thrown here
The exception thrown as follows:
System.NotImplementedException
The member 'IQueryable.Provider' has not been implemented on type
'DbSet`1Proxy_1' which inherits from 'DbSet`1'.
Test doubles for 'DbSet`1' must provide implementations of methods
and properties that are used.
Chances are that you have been using version 4.7.58 of Moq. That particular version was affected by a regression that would have triggered such a NotImplementedException
. That regression has been fixed in version 4.7.63, so I suggest you update your Moq package reference to version 4.7.63 or newer to resolve this issue.
The fact that your code would have worked in Moq versions before 4.7.58 was due to a "feature" which unfortunately caused a lot more problems than it solved. For this reason, that feature was reverted.
Moq has been brought back to its original behaviour, where, in this particular scenario, you need to set up the various interface members via mock.As<TInterface>
before the call to mock.Object
. (Usually, in Moq, it's perfectly fine to perform more set ups even after retrieving the mock object; this scenario is a notable exception. Hopefully this can be fixed in a future version of Moq.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With