Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do mock frameworks work?

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?

like image 895
GurdeepS Avatar asked Feb 16 '10 22:02

GurdeepS


People also ask

How does mock method work?

A object that you want to test may have dependencies on other complex objects. To isolate the behavior of the object you want to test you replace the other objects by mocks that simulate the behavior of the real objects. So in simple words, mocking is creating objects that simulate the behavior of real objects.

What are mock frameworks?

What is a mocking framework? Mocking frameworks are used to generate replacement objects like Stubs and Mocks. Mocking frameworks complement unit testing frameworks by isolating dependencies but are not substitutes for unit testing frameworks.

How do mocking libraries work?

A mocking library allows you to simulate an interface or abstract type's implementation. You instantiate a 'mock' object of the interface, and tell that mock object what it should return if a method/property is called against that mock. You can also assert that a method/property was or wasn't called.


1 Answers

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.

like image 56
Gavin Miller Avatar answered Oct 21 '22 09:10

Gavin Miller