0

Using Microsoft fakes i have the following method signature in my stub object:

 public void GetAllOf1ExpressionOfFuncOfT0M0Int32Int32<T2>(FakesDelegates.Func<Expression<System.Func<T, T2>>, int, int, IList<T>> stub);

which is the stub method for this real one:

  IList<T> GetAll<T2>(Expression<Func<T, T2>> orderbyexpr, int nStartAt = -1, int NumToSel = -1);

What i would like to do is assigning the stub method with custom stuff by using this method:

  public static RunReport StubMethod<T>(ref FakesDelegates.Func<T> func, T returnValue) 
    {
        var counter = new RunReport();

        func = () =>
                   {
                       Interlocked.Increment(ref counter.Count);
                           return returnValue;
                   };
        return counter;
    }

the problem i have is that i cant understand what the signature of StubMethod shoudld be and how can i call it?

i tried couple of thing which led me to "method group cannot be assigned/cannot cast the method group".

BTW - it working perfectly with other simpler stub method like this:

    IList<T> IRepository<T>.GetAll();

so it must be definition issue...

this is where i use GetAll in the code... this should be redirect to my custom function:

  public IList<EntityType> GetAllEntityTypesByName(string filter)
    {
        IList<EntityType> types = new List<EntityType>();
        try
        {
            using (IUnitOfWork uow = ctx.CreateUnitOfWork(true))
            {
                IRepository<EntityType> repType = uow.CreateRepository<EntityType>();
                if (string.IsNullOrEmpty(filter))
                {
                    types = repType.GetAll(o => o.Name);
                }
                else
                {
                    types = repType.Find(t => t.Name.ToLower().Contains(filter.ToLower()), o => o.Name);
                }
            }
        }
        catch (Exception e)
        {
            ResourceManager.Instance.Logger.LogException(e);
        }
        return types;
    }

Another way to look at the problem is that i want to replace this:

stubRepType.GetAllOf1ExpressionOfFuncOfT0M0Int32Int32<string>((a, b, c) => { return new List<EntityType>(); });

with this:

TestHelper.StubRepositoryMethod<IList<EntityType>>(ref stubRepType.GetAllOf1ExpressionOfFuncOfT0M0Int32Int32<string>, new List<EntityType>());
Ori Price
  • 3,593
  • 2
  • 22
  • 37
  • The fourth character on the second line of the seventh file in the solution (ordered by their seventh character and then by the last edit date) is evil. Fun aside, your problem is nigh impossible to solve without seing the code where you actually call your StubMethod. – Nuffin Nov 01 '12 at 14:06

1 Answers1

1

Try using concrete types first. Here is a working example.

[TestMethod]
public void TestMethod1()
{
    var stub = new StubIRepository<DateTime>();
    stub.GetAllOf1ExpressionOfFuncOfT0M0Int32Int32<int>(this.StubGetAll);
    var target = (IRepository<DateTime>)stub;
    IList<DateTime> datesByMonth = target.GetAll(date => date.Month);
    Assert.AreEqual(2005, datesByMonth[0].Year);
}

IList<DateTime> StubGetAll(Expression<Func<DateTime, int>> orderByExpr, int startIndex = -1, int numberOfItems = -1)
{
    var allDates = new[] {
        new DateTime(2001, 12, 1),
        new DateTime(2002, 11, 1),
        new DateTime(2003, 10, 1),
        new DateTime(2004, 9, 1),
        new DateTime(2005, 8, 1)
    };

    Func<DateTime, int> orderByFunc = orderByExpr.Compile();

    return allDates.OrderBy(orderByFunc).ToList();
}
Oleg Sych
  • 6,548
  • 36
  • 34
  • Thanks, but is there a way to make it generic? instead of creating a StubGetAll() method for each type? i want to dynamically assign new implementation to the func on run-time. – Ori Price Nov 04 '12 at 09:30
  • Would you mind spelling out entire the scenario you have in mind in pseudo code? – Oleg Sych Nov 05 '12 at 17:00