mercredi 10 juin 2015

Syntax for shimming an extension method with a generic type and a complex signature?

I'm using Microsoft Fakes and Moq to unit test a service layer written with Entity Framework 6. In the service layer assembly, there is a heavily used extension method that provides add/update functionality to DbSet:

public static TEntity AddOrUpdate<TEntity>(this System.Data.Entity.DbSet<TEntity> dbSet,
            Expression<Func<TEntity, bool>> selectorPredicate, 
            Func<TEntity, bool> changedFunc, 
            Action<TEntity> initialization)
    where TEntity : class, new()
{
}

I have been digging all over and trying all manner of things, but I can't for the life of me figure out what syntax I need to use to shim this thing. The class where the method resides is called ContextExtensions, so I know it starts something like this:

using (ShimsContext.Create())
{
    ShimContextExtensions.AddOrUpdateOf1DbSetOfM0ExpressionOfFuncOfM0BooleanFuncOfM0BooleanActionOfM0<MyModelClass>(....);
}

Other questions I've seen about shimming methods always seem to follow this format (as does stubbing):

ShimPlace.ShimMethod = (args) => { //body of shim };

However, I get an error at my generic parameter (Class name is not valid at this point) when I remove the parentheses from directly after the shim method name. So I started playing with this idea (shortened the method name for all of our sanity):

ShimContextExtensions.AddOrUpdateWithPilesOfArgs<MyModelClass>((args) => { //body of shim });

This results in compiler error "Incompatible anonymous function signature." I would try and decode what the syntax needs to be, but I cannot find any documentation anywhere that describes syntax for a situation like this.

Aucun commentaire:

Enregistrer un commentaire