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

  • Home
  • SEARCH
  • 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 3316520
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T22:28:45+00:00 2026-05-17T22:28:45+00:00

Is there a way to setup a Moq Mock using a string parameter instead

  • 0

Is there a way to setup a Moq Mock using a string parameter instead of an expression? You can do this using the protected extensions for a protected function, but it throws an exception if public. For example, here is how you might setup a protected void function with Moq:

myMock.Protected().Setup("MyProtectedFunction");

I want to be able to do the same thing with a public function. If I can do this I already have some reflection code that can return the names of all of the functions I need to mock, and I can hopeful build on this to avoid having to setup many functions on a huge factory class.

  • 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-17T22:28:45+00:00Added an answer on May 17, 2026 at 10:28 pm

    Interesting problem, this is almost possible to do using expressions. Since the setup method takes an expression, you can build it at runtime.

    The only bit that needs to happen at compile time is casting the expression to the appropriate lambda type based on the return type of the mocked method. Unfortunately Moq does not provide a Setup overload taking naked Expression, otherwise it could be possible to do what you want 100% at runtime.

    public abstract class Fruit
    {
    }
    
    public class Apple :Fruit
    {
    }
    
    public interface IFactory {
        Fruit CreateFruit(string type);
        void VoidMethod(int intParameter);
    }
    
    [TestClass]
    public class UnitTest1 {
        [TestMethod]
        public void TestMethod1() {
            var factoryMock = new Mock<IFactory>(); 
    
            Expression factoryCall = Expression.Lambda(
                Expression.Call(Expression.Variable(typeof(IFactory), "f"), "CreateFruit", new Type[]{}, Expression.Constant("Apple")), 
                Expression.Parameter(typeof(IFactory), "f"));
    
            //factoryMock.Setup(f=>f.CreateFruit("Apple")).Returns(new Apple());
    
            factoryMock.Setup((Expression<Func<IFactory, Fruit>>)factoryCall).Returns(new Apple());
            var ret = factoryMock.Object.CreateFruit("Apple");
            Assert.IsInstanceOfType(ret, typeof(Apple));
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is probably a dumb question, but is there a way to setup a
Is there a way to setup authentication (ala Basic Authentication) without actually setting up
Is there any way how to run the setup.exe bootstrapper generated by VS2008 with
I was wondering if there's a way (even a manual one) to setup an
Is there a way I can set up callbacks on (or automataically log) method
is there way thats i can preselect an item when the page loads or
Is there any way to check whether a file is locked without using a
I am having an issue using the Moq library to mock an Enum within
If I do this: var repository = new Mock<IRepository<Banner>>(); repository.Setup(x => x.Where(banner => banner.Is.AvailableForFrontend())).Returns(list);
I've been using Moq framework in c# for mocking in unit tests however there

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.