title: node.js - mocking modules with jasmine
body: <p>I am trying to write some unit tests for my server side logic that is written using <code>node.js</code> framework. I wanted to use <code>jasmine</code> library for unit testing and mocking because I am a bit familiar with it.</p>
  
  <p>Now, I tried something like this to mock file system <code>read</code> function:</p>
  
  <pre><code>'use strict';
  var loginRedirectMiddleware = require('../server/loginRedirectMiddleware');
  var fs = require('fs');
  var util = require('../server/util');
  
  describe('loginRedirectMiddleware', function(){
    spyOn(fs,'read');
  ...
  });
  </code></pre>
  
  <p>now I am getting an error on the line with <code>spyOn</code> invocation:</p>
  
  <pre><code>  1) loginRedirectMiddleware encountered a declaration exception
     Message:
       TypeError: Cannot call method 'spyOn' of null
     Stacktrace:
       TypeError: Cannot call method 'spyOn' of null
      at null.&lt;anonymous&gt; (/home/adebksi/Nauka/js-linkedin-connector/server_test/loginRedirectMiddlewareSpec.js:11:3)
      at Object.&lt;anonymous&gt; (/home/adebksi/Nauka/js-linkedin-connector/server_test/loginRedirectMiddlewareSpec.js:10:1)
      at Module._compile (module.js:456:26)
      at Object.Module._extensions..js (module.js:474:10)
      at Module.load (/home/adebksi/Nauka/js-linkedin-connector/node_modules/karma/node_modules/coffee-script/lib/coffee-script/coffee-script.js:211:36)
      at Function.Module._load (module.js:312:12)
      at Module.require (module.js:364:17)
      at require (module.js:380:17)
      at Object.jasmine.executeSpecsInFolder (/home/adebksi/Nauka/js-linkedin-connector/node_modules/grunt-jasmine-node/node_modules/jasmine-node/lib/jasmine-node/index.js:151:9)
      at Object.&lt;anonymous&gt; (/home/adebksi/Nauka/js-linkedin-connector/node_modules/grunt-jasmine-node/tasks/jasmine-node-task.js:110:19)
      at Object.thisTask.fn (/home/adebksi/Nauka/js-linkedin-connector/node_modules/grunt/lib/grunt/task.js:78:16)
      at Object.&lt;anonymous&gt; (/home/adebksi/Nauka/js-linkedin-connector/node_modules/grunt/lib/util/task.js:282:30)
      at Task.runTaskFn (/home/adebksi/Nauka/js-linkedin-connector/node_modules/grunt/lib/util/task.js:235:24)
      at Task.&lt;anonymous&gt; (/home/adebksi/Nauka/js-linkedin-connector/node_modules/grunt/lib/util/task.js:281:12)
      at Task.start (/home/adebksi/Nauka/js-linkedin-connector/node_modules/grunt/lib/util/task.js:290:5)
      at Object.grunt.tasks (/home/adebksi/Nauka/js-linkedin-connector/node_modules/grunt/lib/grunt.js:155:8)
      at Object.module.exports [as cli] (/home/adebksi/Nauka/js-linkedin-connector/node_modules/grunt/lib/grunt/cli.js:38:9)
      at Object.&lt;anonymous&gt; (/home/adebksi/.nvm/v0.10.15/lib/node_modules/grunt-cli/bin/grunt:41:20)
      at Module._compile (module.js:456:26)
      at Object.Module._extensions..js (module.js:474:10)
      at Module.load (module.js:356:32)
      at Function.Module._load (module.js:312:12)
      at Function.Module.runMain (module.js:497:10)
      at startup (node.js:119:16)
      at node.js:901:3
  </code></pre>
  
  <p>I am wondering what am I doing wrong?</p>
  
  <p>When I do <code>console.log(typeof fs.read)</code> I am getting <code>function</code> so this property is a function.</p>