Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function Programming and Mock Objects

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.

like image 773
Simon Woods Avatar asked Aug 25 '10 10:08

Simon Woods


People also ask

What are mocks in programming?

In object-oriented programming, a mock object is a simulated object that mimics the behavior of the smallest testable parts of an application in controlled ways.

What is mock object in Scrum?

It consists of instantiating a test-specific version of a software component (typically a class), which insteads of the normal behaviors provides precomputed results, and often also checks that it's invoked as expected by the objects being tested.

What is the use of mock object?

Using mock objects allows developers to focus their tests on the behavior of the system under test without worrying about its dependencies. For example, testing a complex algorithm based on multiple objects being in particular states can be clearly expressed using mock objects in place of real objects.

What are mock objects in testing?

Mock objects are a type of test double. They replace production objects to make for easier and more reliable testing practices. If developers want to use mock objects as part of their testing process, they need to be aware of potential pitfalls that can harm their applications' code.


2 Answers

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.

like image 166
Christoph Avatar answered Sep 27 '22 15:09

Christoph


I think the important thing to think about is the idea of using tests help you to structure your code. Mocks are really about deferring decisions you don't want to take now (and a widely misunderstood technique). Instead of object state, consider partial functions. You can write a function that takes defers part of its behaviour to a partial function that's passed in. In a unit test, that could be a fake implementation that lets you just focus on the code in hand. Later, you compose your new code with a real implementation to build the system.

Actually, when we were developing the idea of Mocks, I always thought of Mocks this way. The object part was incidental.

like image 24
Steve Freeman Avatar answered Sep 27 '22 16:09

Steve Freeman