in reply to Smallish mock objects

Can you explaint to me why mock objects are so great? I've used them, but I find that I sometimes make inappropriate interface assumptions and lose some integration testing as a result (in other words, if my mocked objects have subtle differences from the real ones, my tests might pass but they're not correct). I've always preferred Test::MockModule and just replacing the exact target code that might prove problematic in something I might otherwise mock. This allows me to at least test partial integration with the object in question.

I do realize that some module simple can't be loaded easily (some of the Apache ones, for example), so mock objects would be appropriate there, but otherwise, I don't think it's worth the trouble.

Cheers,
Ovid

New address of my CGI Course.

Replies are listed 'Best First'.
Re^2: Smallish mock objects
by chromatic (Archbishop) on Dec 16, 2006 at 09:37 UTC

    I prefer Test::MockObject::Extends these days. The nice point is just as you say; when you need really precise control either to make something happen that's difficult to invoke normally or to work around something really complex that is difficult to test.

    Complex server initialization or failure types are the two main items that come to my mind.

Re^2: Smallish mock objects
by adrianh (Chancellor) on Dec 16, 2006 at 14:46 UTC
    I do realize that some module simple can't be loaded easily (some of the Apache ones, for example), so mock objects would be appropriate there, but otherwise, I don't think it's worth the trouble.

    I only find myself using them when I have some state in the "mock" object that's hard to reproduce in a "live" object (e.g rare error conditions, etc.) or when I'm dealing with third party code.

    I have to admit I rarely use any of the CPAN modules for mocking and do something like:

    { package MockFoo; sub new { bless {}, shift } sub rarely_returns_42 { return 42 } } my $mock = MockFoo->new;

    rather than:

    use Test::MockObject; my $mock = Test::MockObject->new->set_always( rarely_returns_42 => 42 );

    Probably because I don't use them enough for the API to be familiar to me.

    The pro-/anti-mock argument seems to occur every few months on the TDD mailing list :-) Google around for "state based testing" vs "interaction based testing".

      Your last comment is very interesting. I'll have a look to state based testing vs interaction based testing, thanks

      coming back to the thread, I've always felt that mock objects shine when you *actually* want to be in control:

    • you want to force failures all over the place
    • when you can record "external contexts" so you have state, and you associate your mock object with this external state. Pre-conditions and post-conditions (can be other processes!) can insure invariants. An example would be when your test suite incorporates snapshots of data in a few small (almost) flat databases (say switching on and off at will your big fat Oracle). Needless to say that collecting the data becomes part of the tests.
    • Actually, "internal testing" is the thing most talked about when the real situation is closer to "external testing", I guess. If we look at Chromatic's and Ian Langworth's "perl testing" the ratio is about 10 to one. Even unitary testing if often a mix, and it seems that a generic way to approach "external testing" mechanics would be very useful (if you have pointers about something actually general enough but still practical, IŽll be grateful).

      Maybe I am a paranoid, but I build my systems (even small prototypes) making them fail all over the place. Thinking early about recovery seems to make them better (not really something rational, more like a gut feeling ;)...probably because this over-emphasizes data sources.

      hth --stephan

        Actually I just read Martin Fowler's article "Mocks aren't stubs" http://www.martinfowler.com/articles/mocksArentStubs.html. It's funny as I think I never used the term "mock" correctly...

        The way I understood the concept was: you take an object and you replace it by a proxy: - logical state is obtained by any means consistent with the logical invariants of the object; you can even have "mock" methods that do (almost) nothing...(like failing!)

        So okay, maybe in that sense "mock" methods are not pure stubs given that they must at least reach into something that changes the state of an object.

        In a way, the "final photo" is: you respect the messaging system, and bluntly change object data. Seems like thermodynamics, many paths are possible (and interesting, depending on what you want to test)

        If we focus purely on the external interactions of a program , that particular view that emphasizes state corresponds to treating everything "external" like a database. (In Quantum mechanics this concept is quite close to the concept of "picture" or perspective --Heisenberg picture, Schro"dinger picture, and mixed like the Furry picture-- there testing is the process of getting state but sometimes you want to emphasize the transitions and the "picture" helps then) Then when you start looking for more transparency, and an ORM for example gives you that: the database used is not important anymore; but still the real gain is when you push your abstraction one step further for total transparency: what is left are "datasets" (or set of) with many views (corresponding to different ways of serializing the data). So your database (and the tests!) is say your snapshot of xml before and after any state-changing call... and OO-zealots are just shooting me now;) but hey I still use object-based (black boxes and roles err...interfaces)

        hth --stephan
Re^2: Smallish mock objects
by diotalevi (Canon) on Dec 20, 2006 at 07:25 UTC

    I haven't used mock objects much - most of what I've tested hasn't either required objects or the real things have been easy enough to instantiate in place. Right now it was far easier to just get an object that satisfied $obj->foo == 1 than it was to get the real thing and get it to return true or false. I wasn't testing the entire suite - I just want to test a single function that's getting refactored and need to supply differing boolean values.

    Incidentally, it was Devel::Spy which told me I needed to go treat this method as a boolean. Neat module. Glad I wrote it. I hope I'll get use out of it more than once.

    ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊