title: Jasmine - How To Mock Global Variables & Functions
body: <p>I have a script I would like to test that includes global variables with some of them being initialized with functions calls.  For example</p>
  
  <pre><code>var g_count = 0;
  var g_util = addLibrary( "util.lib" );
  </code></pre>
  
  <p>I would like to mock both <code>g_count</code> and <code>g_util</code></p>
  
  <p>If I have a function call within my testing function I can easily mock it with <code>addLibrary = jasmine.createSpy();</code> but once it is in global scope, the mock fails and I get an error message </p>
  
  <blockquote>
    <p>Reference Error: addLibrary is not defined.</p>
  </blockquote>
  
  <p><strong>Edit:</strong> I figured out how to mock the variable.  I added it to a global property, </p>
  
  <pre><code>var globals = {
      g_count : null
  }
  </code></pre>
  
  <p>Exported it</p>
  
  <pre><code>module.exports = {
      globals: globals
  }
  </code></pre>
  
  <p>Then spied on it</p>
  
  <pre><code>spyOnProperty(util.globals, 'g_count').and.returnValue(5);
  </code></pre>
  
  <p>I still can't figure out the <code>addLibrary</code> function</p>