# Clojure: Testing The Creation Of A Partial Function

DevFeed: [Clojure: Testing The Creation Of A Partial Function](<https://devfeed.tech/articles/clojure-testing-the-creation-of-a-partial-function-31899.md>)

Original publisher: [Read original article](<http://blog.jayfields.com/2013/05/clojure-testing-creation-of-partial.html>)

Author: Jay (noreply@blogger.com)

Published: 2013-05-14T12:00:00Z

Content type: article

Language: en

Sources: [Jay Fields](<https://devfeed.tech/sources/jay-fields.md>)

Topics: [Clojure](<https://devfeed.tech/topics/clojure.md>), [Testing](<https://devfeed.tech/topics/testing.md>), [Refactoring](<https://devfeed.tech/topics/refactoring.md>), [function](<https://devfeed.tech/topics/function.md>)

Tags: [clojure](<https://devfeed.tech/tags/clojure.md>), [clojure-functions](<https://devfeed.tech/tags/clojure-functions.md>), [function](<https://devfeed.tech/tags/function.md>), [refactoring](<https://devfeed.tech/tags/refactoring.md>), [test](<https://devfeed.tech/tags/test.md>), [testing](<https://devfeed.tech/tags/testing.md>)

## AI overview

This Clojure article examines testing a refactoring that replaces stored data with a partially applied function. It explains how to verify the function and arguments using redefinition, while comparing state-based and implementation-focused tests and their maintenance trade-offs.

## Source excerpt

I recently refactored some code that takes longs from two different sources to compute one value. The code originally stored the longs and called a function when all of the data arrived. The refactored version partials the data while it's incomplete and executes the partial'd function when all of the data is available. Below is a contrived example of what I'm taking about. Let's pretend we need a function that will allow us to check whether or not another drink would make us legally drunk in New York City. The code below stores the current bac and uses the value when legally-drunk? is called. The following (passing) tests demonstrate that everything works as expected. This code works without issue, but can also be refactored to store a partial'd function instead of the bac value. Why you would want to do such a thing is outside of the scope of this post, so we'll just assume this is a good refactoring. The code below no longer stores the bac value, and instead stores the pure-legally-drunk? function partial'd with the bac value. Two of the three of the tests don't change; however, the test that was verifying the state is now broken. note: The test output has been trimmed and reformatted to avoid horizontal scrolling. In the output you can see that the test is failing as you'd expect, due to the change in what we're storing. What's broken is obvious, but there's not an obvious solution. Assuming you still want this state based test, how do you verify that you've partial'd the right function with the right value? The solution is simple, but a bit tricky. As long as you don't find the redef too magical, the following solution allows you to easily verify the function that's being partial'd as well as the arguments. Those tests all pass, and should provide security that the legally-drunk? and update-bac functions are sufficiently tested. The pure-legally-drunk? function still needs to be tested, but that should be easy since it's a pure function. Would you want this kind