What functionality should a (basic) mock framework have?

Posted by user1175327 on Programmers See other posts from Programmers or by user1175327
Published on 2013-11-05T11:46:14Z Indexed on 2013/11/05 16:11 UTC
Read the original article Hit count: 286

Filed under:
|

If i would start on writing a simple Mock framework, then what are the things that a basic mock framework MUST have? Obviously mocking any object, but what about assertions and perhaps other things?

When I think of how I would write my own mock framework then I realise how much I really know (or don't know) and what I would trip up on. So this is more for educational purposes.

Of course I did research and this is what i've come up with that a minimal mocking framework should be able to do. Now my question in this whole thing is, am I missing some important details in my ideas?


Mocking

  • Mocking a class: Should be able to mock any class. The Mock should preserve the properties and their original values as they were set in the original class. All method implementations are empty.
  • Calls to methods of Mock: The Mock framework must be able to define what a mocked method must return. IE: $MockObj->CallTo('SomeMethod')->Returns('some value');

Assertions

To my understanding mocking frameworks also have a set of assertions. These are the ones I think are most important (taken from SimpleTest).

expect($method, $args)             Arguments must match if called
expectAt($timing, $method, $args)    Arguments must match when called on the $timing'th time
expectCallCount($method, $count)       The method must be called exactly this many times
expectMaximumCallCount($method, $count)     Call this method no more than $count times
expectMinimumCallCount($method, $count)     Must be called at least $count times
expectNever($method)           Must never be called
expectOnce($method, $args)  Must be called once and with the expected arguments if supplied
expectAtLeastOnce($method, $args)  Must be called at least once, and always with any expected arguments

And that's basically, as far as I understand, what a mock framework should be able to do. But is this really everything? Because it currently doesn't seem like a big deal to build something like this.

But that's also the reason why I have the feeling that i'm missing some important details about such a framework.

So is my understanding right about a mock framework? Or am i missing alot of details?

© Programmers or respective owner

Related posts about php

Related posts about mocking