title: Writing tests for functional code?
body: <p>For the sake of example, suppose we're building a simple cache. We can take the functional approach using a closure:</p>
  
  <pre><code>makeCache = -&gt;
    c = {}
    (key, fetch) -&gt;
      if c[key]
        c[key]
      else
        c[key] = fetch()
        c[key]
  
  cache = makeCache()
  cache(1, -&gt; 1)
  </code></pre>
  
  <p>The problem I find with this approach is that you can't test it easily. I want to be able to check that the <code>'1'</code> is in the cache. If I add more values, I want to make sure they are in the cache as well. </p>
  
  <p>If I have a more complicated cache, like an LRU cache, I would want to test other aspects as well. However those variables are entirely hidden behind a closure. </p>
  
  <p>I tend to think up solutions using a closures but then I resort to an object oriented style so that its testable:</p>
  
  <pre><code>class Cache
    constructor: -&gt;
      @c = {}
    get: (key, fetch) -&gt;
      if c[key]
        c[key]
      else
        c[key] = fetch()
        c[key]
  
  cache = new Cache()
  cache.get(1, -&gt;1)
  </code></pre>
  
  <p>Now I have access to <code>cache.c</code> and any other variables that I need to inspect to test.</p>
  
  <p>So my question is, how can you test your code when using a functional style closure?</p>