tags:

views:

31

answers:

1

Hi

I was recently watching a webcast on Clojure. In it the presenter made a comment in the context of discussing the FP nature of Clojure which went something like (I hope I don't misrepresent him) "Mock objects are mocking you".

I also heard a similar comment a while back when I watched a webcast when Microsoft's Reactive Framework was starting to appear . It went something like "Mock objects are for those who don't know math")

Now I know that both comments are jokes/tongue-in-cheek etc etc (and probably badly paraphrased), but underlying them is obviously something conceptual which I don't understand as I haven't really made the shift to the FP paradigm.

So, I would be grateful if someone could explain whether FP does in fact render mocking redundant and if so how.

Many thx

Simon

+1  A: 

In pure FP you have referentially transparent functions that compute the same output every time you call them with the same input. All the state you need must therefore be explicitly passed in as parameters and out as function results, there are no stateful objects that are in some way "hidden behind" the function you call. This, however, is, what your mock objects usually do: simulate some external, hidden state or behavior that your subject under test relies on.

In other words: OO: Your objects combine related state and behavior. Pure FP: State is something you pass between functions that by themselves are stateless and only rely on other stateless functions.

Christoph
OK. Thx. But presumably the state being passed between functions around could be in mock objects or 'real objects' so mock objects would then still be appropriate for testing an FP solution - no?
Simon Woods
In pure FP, there are no objects in the OO sense. There are data structures, which may be user defined. In some languages there are distant relatives of interfaces: Haskell has type classes, which are basically a set of functions that must be defined for every data type in that type class. So yes, you could write your function to operate on data types of a certain type class and test it on another data type than the one you use in production.In F# or Scala, where you can actually manipulate .NET or Java-Objects in a functional style, it may be even be necessary to use actual mock objects.
Christoph