I'm having a problem creating a unit test for a method that's calling a static function and one of its parameters is a func. The problem still occurs even though I've created a wrapper class to call the static function i.e extension, as suggested in this article:
How do I use Moq to mock an extension method?
As suggested by someone, I should create an interface and a wrapper class which I did but I'm still getting an exception when it's hitting the moq setup. My setup looks as follows:
context.Setup(c => new MyExtensionWapper()
.UpdateCollection<TestObject, int>(
context.Object,
databaseCollection,
detachedCollection,
o=>o.Id.Value));
The error occurs on the o=>o.Id.Value
and the class MyExtensionWapper is the Wrapper function which implements an IExtensions with the UpdateCollection contract.
Note that my UpdateCollection is a generic function related to Entity Framework
where TEntity
is an EF entity object & and the TKey
tends to be an Int
.
public class MyExtensionWapper : IExtensions
{
public virtual void UpdateCollection<TEntity, TKey>(IDbContext context,
ICollection<TEntity> databaseCollection,
ICollection<TEntity> detachedCollection,
Func<TEntity, TKey> keySelector)
where TEntity : class
where TKey : IEquatable<TKey>
{
context.UpdateCollection(databaseCollection,
detachedCollection, keySelector);
}
}
When it hits the moq context.Setup(...
, I get the following error:
System.NotSupportedException: 'Unsupported expression: o => o.Id.Value'
Can you help?
Thanks.
UPDATE-1
This is my original call where I tried to call a static method (extension) directly from within the mock setup:
context.Setup(c => c.UpdateCollection<TestObject, int>(
databaseCollection,
detachedCollection,
o=>o.Id.Value));
and my mock is created as follows:
context = CreateMock<IDbContext>(MockBehavior.Loose);
But when called I get the following error:
System.NotSupportedException: 'Invalid setup on an extension method:....
Update 2
As suggested by @Johnny, I should mock the interface, so I tried the following but I'm getting the same error:
Mock<IExtensions> dbExtension;
// Note: Results are the same whether I create the mock with a loose
// behaviour or not.
dbExtension = CreateMock<IExtensions>(MockBehavior.Loose);
dbExtension.Setup(c => new MyExtensionWapper()
.UpdateCollection<TestObject, int>(
context.Object,
databaseCollection,
detachedCollection,
o=>o.Id.Value,
Also rather than calling the wrapper class, I tried to call the UpdateCollection method directly from the mock since it is defined in the interface i.e. c=>c.UpdateCollection(databaseCollection, ...) but to no avail.