Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6201929
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T04:38:30+00:00 2026-05-24T04:38:30+00:00

I want to create a factory that will create commonly mocked objects for my

  • 0

I want to create a factory that will create commonly mocked objects for my unit tests. I’ve already managed to set up my tests so I can mock up a Linq2Sql DataContext and return an in memory table instead of hitting the database. I set it up like this:

_contactsTable = new InMemoryTable<Contact>(new List<Contact>());
_contactEmailsTable = new InMemoryTable<ContactEmail>(new List<ContactEmail>());
//  repeat this for each table in the ContactsDataContext

var mockContext = new Mock<ContactsDataContext>();
mockContext.Setup(c => c.Contacts).Returns(_contactsTable);
mockContext.Setup(c => c.ContactEmails).Returns(_contactEmailsTable);
// repeat this for each table in the ContactsDataContext

This gets tedious if the DataContext contains a lot of tables, so I thought a simple factory method that used reflection to get all the tables off the DataContext might help:

public static DataContext GetMockContext(Type contextType)
{
    var instance = new Mock<DataContext>();
    var propertyInfos = contextType.GetProperties();
    foreach (var table in propertyInfos)
    {
        //I'm only worried about ITable<> now, otherwise skip it
        if ((!table.PropertyType.IsGenericType) ||
            table.PropertyType.GetGenericTypeDefinition() != typeof (ITable<>)) continue;

        //Determine the generic type of the ITable<>
        var TableType = GetTableType(table);
        //Create a List<T> of that type 
        var emptyList = CreateGeneric(typeof (List<>), TableType);
        //Create my InMemoryTable<T> of that type
        var inMemoryTable = CreateGeneric(typeof (InMemoryTable<>), TableType, emptyList);  

        //NOW SETUP MOCK TO RETURN THAT TABLE
        //How do I call instance.Setup(i=>i.THEPROPERTYNAME).Returns(inMemoryTable) ??
    }
return instance.Object;
}

So far I’ve figured out how to create the objects I need to setup for the Mock, but I just can’t figure out how to dynamically call Moq’s Setup() passing in the property names. I started looking at reflection to Invoke() Moq’s Setup() method, but it got really ugly fast.

Does anyone have a simple way to dynamically call Setup() and Returns() like this?

Edit: Brian’s answer got me there. Here’s how it works:

public static DataContext GetMockContext<T>() where T: DataContext
    {
        Type contextType = typeof (T);
        var instance = new Mock<T>();
        var propertyInfos = contextType.GetProperties();
        foreach (var table in propertyInfos)
        {
            //I'm only worried about ITable<> now, otherwise skip it
            if ((!table.PropertyType.IsGenericType) ||
                table.PropertyType.GetGenericTypeDefinition() != typeof(ITable<>)) continue;

            //Determine the generic type of the ITable<>
            var TableType = GetTableType(table);
            //Create a List<T> of that type 
            var emptyList = CreateGeneric(typeof(List<>), TableType);
            //Create my InMemoryTable<T> of that type
            var inMemoryTable = CreateGeneric(typeof(InMemoryTable<>), TableType, emptyList);

            //NOW SETUP MOCK TO RETURN THAT TABLE
            var parameter = Expression.Parameter(contextType);
            var body = Expression.PropertyOrField(parameter, table.Name);
            var lambdaExpression = Expression.Lambda<Func<T, object>>(body, parameter); 

            instance.Setup(lambdaExpression).Returns(inMemoryTable);
        }
        return instance.Object;
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T04:38:31+00:00Added an answer on May 24, 2026 at 4:38 am

    What you are looking for are Linq Expressions. Here is a sample of building a property accessory expression in action.

    Using this class:

    public class ExampleClass
    {
       public virtual string ExampleProperty
       {
          get;
          set;
       }
    
       public virtual List<object> ExampleListProperty
       {
          get;
          set;
       }
    }
    

    The following tests demonstrate accessing its properties dynamically using the Linq.Expression classes.

    [TestClass]
    public class UnitTest1
    {
       [TestMethod]
       public void SetupDynamicStringProperty()
       {
          var dynamicMock = new Mock<ExampleClass>();
    
          //Class type
          var parameter = Expression.Parameter( typeof( ExampleClass ) );           
          
          //String rep of property
          var body = Expression.PropertyOrField( parameter, "ExampleProperty" ); 
    
          //build the lambda for the setup method
          var lambdaExpression = Expression.Lambda<Func<ExampleClass, object>>( body, parameter );
    
          dynamicMock.Setup( lambdaExpression ).Returns( "Works!" );
    
          Assert.AreEqual( "Works!", dynamicMock.Object.ExampleProperty );
       }
    
       [TestMethod]
       public void SetupDynamicListProperty_IntFirstInList()
       {
          var dynamicMock = new Mock<ExampleClass>();
    
          var parameter = Expression.Parameter( typeof( ExampleClass ) );
          var body = Expression.PropertyOrField( parameter, "ExampleListProperty" );
          var lambdaExpression = Expression.Lambda<Func<ExampleClass, object>>( body, parameter );
    
          var listOfItems = new List<object> { 1, "two", DateTime.MinValue };
          dynamicMock.Setup( lambdaExpression ).Returns( listOfItems );
    
          Assert.AreEqual( typeof( int ), dynamicMock.Object.ExampleListProperty[0].GetType() );
          Assert.AreEqual( 1, dynamicMock.Object.ExampleListProperty[0] );
    
          Assert.AreEqual( 3, dynamicMock.Object.ExampleListProperty.Count );
       }
    
       [TestMethod]
       public void SetupDynamicListProperty_StringSecondInList()
       {
          var dynamicMock = new Mock<ExampleClass>();
    
          var parameter = Expression.Parameter( typeof( ExampleClass ) );
          var body = Expression.PropertyOrField( parameter, "ExampleListProperty" );
          var lambdaExpression = Expression.Lambda<Func<ExampleClass, object>>( body, parameter );
    
          var listOfItems = new List<object> { 1, "two" };
          dynamicMock.Setup( lambdaExpression ).Returns( listOfItems );
    
          Assert.AreEqual( typeof( string ), dynamicMock.Object.ExampleListProperty[1].GetType() );
          Assert.AreEqual( "two", dynamicMock.Object.ExampleListProperty[1] );
    
          Assert.AreEqual( 2, dynamicMock.Object.ExampleListProperty.Count );
       }
    }
    

    EDIT

    You are taking a step too far with this code. This code is creating a method with the signature of the lambda you want and then it’s executing it (.Invoke). You are then trying to pass the result of the object (hence the compile error) into the setup for Moq. Moq will do the method execution and hookup for you once you tell it how to act (hence the lambda). If you use the lambda expression creation that I provided, it will build what you need.

    var funcType = typeof (Func<>).MakeGenericType(new Type[] {TableType, typeof(object)});
    
    var lambdaMethod = typeof (Expression).GetMethod("Lambda");
    var lambdaGenericMethod = lambdaMethod.MakeGenericMethod(funcType);
    var lambdaExpression = lambdaGenericMethod.Invoke(body, parameter);
    
    //var lambdaExpression = Expression.Lambda<Func<ExampleClass, object>>(body, parameter); // FOR REFERENCE FROM BRIAN'S CODE
    instance.Setup(lambdaExpression).Returns(inMemoryTable);
    

    Do this instead

    var parameter = Expression.Parameter( TableType );
    var body = Expression.PropertyOrField( parameter, "PutYourPropertyHere" );
    var lambdaExpression = Expression.Lambda<Func<ExampleClass, object>>( body, parameter );
    
    instance.Setup(lambdaExpression).Returns(inMemoryTable);
    

    EDIT

    Took a stab at correcting the GetMockContext. Please note the few changes (I marked each line). I think this is closer. I am wondering, does InMemoryTable inherit from DataContext? If not, the method signature will be incorrect.

    public static object GetMockContext<T>() where T: DataContext
    {
        Type contextType = typeof (T);
        var instance = new Mock<T>();  //Updated this line
        var propertyInfos = contextType.GetProperties();
        foreach (var table in propertyInfos)
        {
            //I'm only worried about ITable<> now, otherwise skip it
            if ((!table.PropertyType.IsGenericType) ||
                table.PropertyType.GetGenericTypeDefinition() != typeof(ITable<>)) continue;
    
            //Determine the generic type of the ITable<>
            var TableType = GetTableType(table);
            //Create a List<T> of that type 
            var emptyList = CreateGeneric(typeof(List<>), TableType);
            //Create my InMemoryTable<T> of that type
            var inMemoryTable = CreateGeneric(typeof(InMemoryTable<>), TableType, emptyList);
    
            //NOW SETUP MOCK TO RETURN THAT TABLE
            var parameter = Expression.Parameter(contextType);
            var body = Expression.PropertyOrField(parameter, table.Name);
            var lambdaExpression = Expression.Lambda<Func<T, object>>(body, parameter); 
    
            instance.Setup(lambdaExpression).Returns(inMemoryTable);
        }
        return instance.Object; //had to change the method signature because the inMemoryTable is not of type DataContext. Unless InMemoryTable inherits from DataContext?
    }
    

    I hope this helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create a factory which will create a smack XMPPConnection. The factory
I am trying to write a factory method that will create a derived instance
I want to create a servlet that will allow me to upload image files
I want to create a function that performs a function passed by parameter on
I want to create a simple http proxy server that does some very basic
I want to develop a multi-server clustered framework that will work similarly to the
I have a factory called ProductFactory I can create a product like so: $product
I have a class representing a set of values that will be used as
In C++ for Windows, I have some object factory that is supposed to create
I want to use a temp directory that will be unique to this build.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.