Aaron Gates
Aaron Gates

Reputation: 469

Pass property of generic class as parameter in AutoMoq

What I'm trying to do is generalize making repositories using AutoFixture and Moq. I have a method called 'add' that adds fake records to a list. The list is called records and is global to the class. The generic M is a model to be mocked. The method returns 'this' so the method can be chained.

public Repo<M> add(string prop, string val) {
    var a = fixture.Build<M>().With(m => m.GetProperty(prop), val).Create();
    records.Add(a);
    return this;
}

with the extension class (I found this searching SO):

public static class MyExtensions
{
    public static object GetProperty<T>(this T obj, string name) where T : class
    {
        Type t = typeof(T);
        return t.GetProperty(name).GetValue(obj, null);
    }
}

The error I'm getting is "ArgumentException occurred in Ploeh.AutoFixture.dll".

My question is: How can one pass a property of a generic object in as a parameter on a method when the model is unknown at compile time?

Upvotes: 0

Views: 579

Answers (2)

Aaron Gates
Aaron Gates

Reputation: 469

Many thanks to Arkadiusz for the correct answer. I just want to add this method I added (inspired by several SO posts) to create the Expression so that the usage would be: add("username","me")

private Expression<Func<M,T>> makeExpression<T>(string prop, T val)
{
    var m = Expression.Parameter(typeof(M), "m");
    Expression columnExpr = Expression.Property(m,typeof(M).GetProperty(prop));

    return Expression.Lambda<Func<M, T>>(columnExpr, m);
}

Then, in the add method, I changed propertyPicker to makeExpression(property,val), property being the name of the property as a string.

Upvotes: 0

Arkadiusz K
Arkadiusz K

Reputation: 1827

With method requires Expression<Func<T, TProperty>> parameter not PropertyInfo.

You can change your add method to take Expression<Func<T, TProperty>> instead of string :

public Repo<M> add<T>(Expression<Func<M, T>> propertyPicker, T val) 
{
   var a = fixture.Build<M>().With(propertyPicker, val).Create();
   records.Add(a);
   return this;
}

and this is the usage :

add(foo => foo.Name, "abc");

Upvotes: 4

Related Questions