title: Unit Testing local function in Lua
body: <p>So I'm using Busted to create unit tests for an existing Lua file, without changing the code in the file if possible. The file imports another file, and then stores various methods from that file in local functions, like so.</p>
  
  <pre><code>[examplefile.lua]
  local helper = require "helper.lua"
  local helper_accept = helper.accept
  local helper_reject = helper.reject
  
  foo = new function()
    -- do something which uses helper_accept
    -- do something which uses helper_reject
  end
  </code></pre>
  
  <p>I want to spy on these methods in my tests to ensure that they have been called at the right places. However, I can't find any way to do this from the test.
  I've tried simply mocking out the helper methods, as in:</p>
  
  <pre><code>[exampletest.lua]
  
  local helper = require "helper.lua"
  local examplefile = require "examplefile.lua"
  
  -- mock the helper function to simply return true
  helper.accept = new function() return true end
  spy.on(helper, "accept")
  examplefile:foo
  assert.spy(helper).was().called()
  </code></pre>
  
  <p>but that doesn't work as the real file uses the helper_accept and helper_reject methods, not helper.accept and helper.reject. </p>
  
  <p>Can this be done without changing the code? 
  Thanks. </p>