title: Mock code in another module
body: <p>I want to mock Amazon <strong>AWS S3</strong> <code>getObject</code></p>
  
  <p>The code I want to test is the following one: Its in <code>helper.js</code></p>
  
  <pre><code>var AWS = require('aws-sdk');
  var s3 = new AWS.S3();
  
  exports.get_data_and_callback = function(callback, extra){
    s3.getObject( {Bucket: SRC_BUCKET, Key: SRC_KEY},
      function (err, data) {
        if (err != null) {
          console.log("Couldn't retrieve object: " + err);
        }else{
          console.log("Loaded " + data.ContentLength + " bytes");
  
          callback(data, extra);
        }
    });
  }
  </code></pre>
  
  <p>In <code>test/helper_test.js</code> <strong>I wrote a test that should mock</strong> the module <strong>AWS</strong></p>
  
  <pre><code>var assert = require('assert');
  var mockery = require('mockery');
  
  describe("helper", function() {
  
    it('loads and returns data from S3 to a callback', function(){
      mockery.enable();
  
      var fakeaws = {
        S3: function(){
            return {
              getObject: function(params, callback){
              callback(null, "hello")
            }
          }
        }
      }
      mockery.registerSubstitute('aws-sdk', fakeaws);
  
      function replace_function(err, data){
        console.log(data);
      }
  
      require('../helper.js').get_data_and_callback(replace_function, null);
  
    });
  });
  </code></pre>
  
  <p>When I require <strong>AWS</strong> in the <strong>Test-File</strong> <code>test/helper_test.js</code> like this:</p>
  
  <pre><code>aws = require('aws-sdk');
  s3 = new aws.S3;
  s3.getObject(replace_function)
  </code></pre>
  
  <p>Then my code works, it prints out <code>hello</code>.</p>
  
  <p><strong>BUT</strong> the execution of <code>require('../helper.js').get_data_and_callback(replace_function, null);</code></p>
  
  <p>Doesn't work like expected, <strong>AWS</strong> stays the same its not replaced with my <strong>fakeaws</strong>. What do I wrong? Do you maybe have other solutions to replace <strong>S3</strong> Thanks</p>