title: How do I test if a function calls a specific method/function?
body: <p>Is there a way in Mocha to test if a function calls a specific method or external function?</p>
  <p><em>I am using Mocha with Chai, but am open to any other assertion libraries.</em></p>
  <hr />
  <p>Ok, so testing whether a methid is being called is pretty easy using sinon. I'm not sure about testing to see if an external function is being called though. So I updated the examples to represent something a little more &quot;real world&quot;. I am working on a node app, so <code>foo.js</code> and <code>bar.js</code> are both modules.</p>
  <h2>Example:</h2>
  <h3>foo.js</h3>
  <pre><code>var bar = require('bar');
  var xyz = function () {};
  
  var Foo = module.exports = function () {
    this.bar();
    bar();
    xyz();
  };
  Foo.prototype.bar = function () {};
  </code></pre>
  <h3>bar.js</h3>
  <pre><code>var bar = module.exports = function () {};
  </code></pre>
  <h3>fooSpec.js</h3>
  <pre><code>var chai      = require('chai');
  var sinon     = require('sinon');
  var sinonChai = require('sinonChai');
  var expect    = chai.expect;
  var Foo       = require('../lib/foo');
  
  chai.use('sinonChai');
  
  describe('Foo', function () {
  
    var method;
  
    beforeEach(function (done) {
      method = sinon.spy(Foo.prototype, 'bar');
      done();
    });
    afterEach(function (done) {
      method.restore();
      done();
    });
  
    it('should call Foo.prototype.bar() immediately', function () {
  
      new Foo();
      expect(method).to.have.been.called;
      
    });
  
    it('should call the module bar immediately', function () {
      // ????????????
    });
  
    it('should call xyz() immediately', function () {
      // ????????????
    });
  });
  </code></pre>
  <p>So as you can see I've figured out how to test for <code>Foo.prototype.bar</code>, but I can't find a way to implement the second and third tests.</p>