---
title: "Discussion on \"Purify Your Tests\" | Hashnode"
description: "Discussion on \"Purify Your Tests\". Here's a fun little trick to purify your tests. When I say \"purify\" I mean it in the pure functional programming sense of making the tests side-effect-free. But more specifically, I'm going to show you a way to avoid mutable mocks.\nTesting Side-Effec..."
url: "https://hashnode.com/posts/2024-05-09-purify-tests/6640b4748af5c5502af37336"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [Purify Your Tests](https://ncreep.hashnode.dev/2024-05-09-purify-tests?utm_source=hashnode&utm_medium=feed)

Here's a fun little trick to purify your tests. When I say "purify" I mean it in the pure functional programming sense of making the tests side-effect-free. But more specifically, I'm going to show you a way to avoid mutable mocks. Testing Side-Effec...

May 12, 2024 · 11 min read · ncreep.hashnode.dev  
Updated Feb 8  
Read the article

-   [#functional-programming](https://hashnode.com/tag/functional-programming)
-   [#scala](https://hashnode.com/tag/scala)
-   [#testing](https://hashnode.com/tag/testing)

## Written by

[Daniel Beskin](https://hashnode.com/@ncreep)

@ncreep

  
[View profile](https://hashnode.com/@ncreep)

This mind intentionally left blank

I enjoy using Scala and Functional Programming to craft code that is beautiful, maintainable, and fun to write. Reach out for software development projects, consulting, and training. https://linksta.cc/@ncreep https://traverse.consulting Buy me a coffee

## Responses 1

Markdown supported · ⌘↵ to post

[Edmund Leif](https://hashnode.com/@eddy557)

[Apr 16, 2025](https://hashnode.com/posts/2024-05-09-purify-tests/6640b4748af5c5502af37336/comment/67ff1b3f390c3277b49a5d98)

It is impossible for me to adequately thank Supreme Peregrine Recovery for their amazing support. Upon learning that my close friend had stolen my phone to gain access to private work data, I felt utterly deceived and devastated. Nevertheless, the staff at Supreme Peregrine Recovery took over and gave me the knowledge I had lost. They showed professionalism and understanding as soon as I contacted them. They helped me locate my stolen phone by using sophisticated recovery methods that I never would have imagined were possible. Their commitment to assisting me in recovering my device and safeguarding my data was absolutely astounding. I was able to retrieve my phone and protect my private information because of their prompt action and careful investigation. In addition to giving me my peace of mind back, Supreme Peregrine Recovery assisted me in handling a very trying circumstance with quickness and kindness. I heartily endorse their services to anyone dealing with such problems. +1,8,7,0,2,2,6,0,6,5,9 supremeperegrinerecovery567(@)zohomail(.)com supremeperegrinerecovery(@)proton(.)me info(@)supremeperegrinerecovery(.)com

[Miklos S](https://hashnode.com/@smiklos)

[Dec 20, 2024](https://hashnode.com/posts/2024-05-09-purify-tests/6640b4748af5c5502af37336/comment/6765796c7d320d9bb8490705)

Great Series Daniel! In general I think code where order of execution matters should be tested by mocks or via your purified methods. However, often the code interacting with side effects is a use case or a controller. Wouldn't it be better to focus on purism in domain logic and test these plumbing components in a more functional / full blown way? E.g in-process testing by using real dependencies. You can cover a whole business flow in a single test to just see if all components were called or not. Now I know you add more logic to this in the next articles but doing so will make changes less convenient.

On another note, with this method of 'leaking' side effecting code's result in the use case I see two problems.

1.  is that often we rely on properties of the loaded data/entity to make optional sideeffecting call (e.g isSubscribed -> send Email also). So in your example we could fix that by using some type bounds but it will make the testing part more awkward.
2.  We also typically want to return a DTO instead of the actual persisted value(s) in which case it again will be hard to work with this code.

What do you think?

[Daniel Beskin](https://hashnode.com/@ncreep)

[Dec 22, 2024](https://hashnode.com/posts/2024-05-09-purify-tests/6640b4748af5c5502af37336/comment/6765796c7d320d9bb8490705#reply-6768417ca8f1e80269e08b06)

Thanks for the positive feedback!

-   Whether you want to write unit tests or full blown functional tests for a component is really a matter of choosing the right tradeoff for your use-case. I find that the maintenance burden of more "realistic" tests tends to be higher, so I have a limited (but non-zero) budget for them. Unit tests (especially purified ones) seem to have a lower maintenance cost, and are relatively easy to write and modify. Purification also helps to make them more resilient to change as it helps narrow down the scope of the test to the exact bits the flow is responsible for. Although in general the tradeoff is really context dependent, in this example where I only test the functionality of a single service (as opposed to the interaction of multiple services), I would gravitate more towards the unit approach. I also find that writing tests like this, or at least thinking in terms of "purified" code helps me crystalize the purpose of the component.
    
-   You're right in calling this code "leaking", it indeed exposes some internal details of the process that you might want to keep hidden. In the second part of the series I modify the code to "leak" less. I might actually add another (small) article about how to hide some of the type parameters.
    
-   For question 1, there's no issue with conditional logic here, as you can choose the type parameter to be distinct things in the production flow vs. the test. For example, `Bookkept` can be `Unit` in production and `Option[String]` in the test. Where `None` will signify that the service decided not to do the bookkeeping flow.
    
-   For question 2, it depends on how tightly coupled your code is with the DTO. From my point of view, orchestrator code like `UberService` shouldn't really "care" too much about the specific data types flowing through it. In part 3 I take advantage of that and use even more type parameters to remove any coupling between `UberService` and the data flowing through it. If you read part 3, let me know if that solves the issue you're concerned with. But basically, for every DTO flowing through the code, I might introduce a type parameter to stand for it, and then mock it out in the test with some simple thing like `String`.
    

Thanks for the interesting comments
