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 7977789
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T09:18:32+00:00 2026-06-04T09:18:32+00:00

Im using Moq to create mocks of a data set. I have created a

  • 0

Im using Moq to create mocks of a data set.

I have created a little helper class that allows me to have an in memory storage instead of a database that makes unit testing a breeze. That way I can add and remove items from my mock data set, this allows me to test my insert and delete service calls.

During the setup of the mock I have a line that looks like the following

this.Setup(i => i.AcademicCycles).Returns(mockStore.GetList<AcademicCycle>());

My mock has a lot of properties so I would like to perform this setup step using reflection. I have managed to the Returns part of the process working via reflection but I am stuck on the lambda method to Setup.

Setup takes an

Expression<Func<GoalsModelUnitOfWork, IQueryable<AcademicCycle>>> that corresponds to the i => i.AcademicCycles

and I would like to create this dynamically. Using reflection I have the following:

The name of the property: “AcademicCycles”

The type IQueryable<AcademicCycle>

The type AcademicCycle

I also have the instance of the i in the lambda statement which is a GoalsModelUnitOfWork

  • 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-06-04T09:18:34+00:00Added an answer on June 4, 2026 at 9:18 am

    The code to create the expression dynamically would be like this:

    ParameterExpression parameter = Expression.Parameter(typeof (GoalsModelUnitOfWork), "i");
    MemberExpression property = Expression.Property(parameter, "AcademicCycles");
    
    var queryableType = typeof (IQueryable<>).MakeGenericType(typeof (AcademicCycle));
    var delegateType = typeof (Func<,>).MakeGenericType(typeof (GoalsModelUnitOfWork), queryableType);
    
    var yourExpression = Expression.Lambda(delegateType, property, parameter);
    

    The result will have the desired type, but the problem is that the return type of Expression.Lambda() is LambdaExpression and you can’t perform a type cast to Expression<Func<...>> to pass it as parameter to your setup function because you don’t know the generic type parameters for the Func. So you have to invoke the Setup method by reflection, too:

    this.GetType().GetMethod("Setup", yourExpression.GetType()).Invoke(this, yourExpression);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using Moq for generation of Stubs and Mocks in my unit tests, I have
I have been using Moq and initially Rhino Mocks over the last year together
i have just started using Moq ver (3.1) and i have read blogs and
I want to mock only the GetValue method of the following class, using Moq:
I'm brand new to Moq (using v 4) and am struggling a little with
I have been writing unit tests using NUnit and Moq with my Silverlight code
I am using constructor injection pattern to insert my mocks with moq. So I
I'm implementing an oauth provider using DotNetOpenAuth CTP library. So I have created an
I've created a unit test that tests interactions on my ViewModel class in a
[using Moq] I am trying to mock a concrete class and mock a virtual

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.