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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T19:27:46+00:00 2026-05-13T19:27:46+00:00

If I was to write a mocking library, how would this work (in other

  • 0

If I was to write a mocking library, how would this work (in other words, how do “they work?)?

One of the things which I wonder is that you are always setting expectations so really you need to compare the expectation to what the method does at runtime, so I assume reflection (resolving types at runtime) is required.

Also, when using the term “mock object”, is the object stubbed out or would it be an object with pre-set expectations?

When I think how I would write my own implementation of a framework/technique, like mock objects, I realise how much I really know (or don’t know) and what I would trip up on: If the mock object is pre-programmed to return set expectations and you don’t call the actual real object, then wouldn’t the result always be the same? Eg:

[TestMethod, Isolated]
public void FakeReturnValueByMethodArgs()
{
    var fake = Isolate.Fake.Instance<ClassToIsolate>();
    // MethodReturnInt will return 10 when called with arguments 3, "abc"
    Isolate.WhenCalled(()=> fake.MethodReturnInt(3,   "     abc")).WithExactArguments().WillReturn(10);
// MethodReturnInt will return 50 when called with arguments 3, "xyz"
    Isolate.WhenCalled(()=> fake.MethodReturnInt(3, "xyz")).WithExactArguments().WillReturn(50);

     Assert.AreEqual(10, fake.MethodReturnInt(3, "abc"));
    Assert.AreEqual(50, fake.MethodReturnInt(3, "xyz"));

}

Wouldn’t this always return true?

  • 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-13T19:27:47+00:00Added an answer on May 13, 2026 at 7:27 pm

    The idea with mocking frameworks is to mock out dependencies, and not the actual classes under test. For your example, your test will always return true, because really you’re only testing the mocking framework and not your actual code!

    A real world mock would look more like this:

    [TestMethod, Isolated]
    public void FakeReturnValueByMethodArgs() {
        var fake = Isolate.Fake.Instance<DependencyClass>();
        // MethodReturnInt will return 10 when called with arguments 3, "abc"
        Isolate.WhenCalled(()=> fake.MethodReturnInt(3, "abc")).WithExactArguments().WillReturn(10);
    
        var testClass = new TestClass(fake);
        testClass.RunMethod();
    
        // Verify that the setup methods were execute in RunMethod()
        // Not familiar with TypeMock's actual method to do this...
        IsolatorExtensions.VerifyInstanceWasCalled(fake);  
    
        // Or assert on values
        Assert.AreEqual(10, testClass.AProperty);
    }
    

    Notice how the mock is passed into the TestClass and a method run on it.

    You can read The Purpose of Mocking to get a better idea of how mocking works.


    Update: Explanation why you’re testing only the mocking framework:

    What you’ve done is create a method MethodReturnInt with the mocking framework using Isolate.WhenCalled(). When you call MethodRecturnInt in the Assert, the code will run the delegate () => fake.MethodReturnInt() and return 10. The mocking framework is effectively creating a method (albeit dynamically) that would look something like this:

    public void MethodReturnInt(int value, string value2) {
        Assert.Equal(3, value);
        Assert.Equal("abc", value2);
        return 10;
    }
    

    It’s a bit more complicated than that, but this is the general idea. Since you never run any code other than the creation of 2 methods and then asserts on those two methods, you’re not testing your own code and therefore only testing the mocking framework.

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

Sidebar

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.