Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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?

share|improve this question
 
What about a mocked method returning a value? –  AndyBursh Nov 5 '13 at 11:53
 
@AndyBursh I think i have that covered already. Read the part Calls to methods of Mock. Or do you mean something different? –  user1175327 Nov 5 '13 at 12:02
 
Ah yes, sorry, I missed that (somehow) –  AndyBursh Nov 5 '13 at 12:16
add comment

1 Answer

A mocking framework should be able to do anything and everything. There should be no question of what it can do, only of what it can do easily. If a certain test is appropriate and the mocking framework lacks the features necessary to make that test possible, then there is serious danger that the test won't happen. People will sometimes prefer to leave something untested rather than switching mocking frameworks or implementing mock objects from scratch.

Therefore, in addition to all the convenient assertions that the mocking framework may have, it should also give the user access to a convenient representation of everything that happened during the test so that users can write their own assertions to test things that you can't even imagine. If you are hiding things from the user then something is seriously wrong: the job of a mocking framework is to present the user with what happened to the mock objects in a useful and intuitive way, not to conceal things.

If you force the user to make assertions in the form of a list of expected method calls to mock objects, then you are forcing the test to be overspecified and denying the user the power to test exactly the thing that needs to be tested. The testing should never be forced to compromise to meet the needs of the mocking framework. Always expand the user's capabilities; never diminish them.

Focus on making sure that your mocking framework will never prevent anything from being tested, then work on making the common use cases easy.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.