title: How can mocha know when event fired on a Controller $scope using $broadcast inside of a $promise?
body: <p>I could not find any other question/answer that met my needs, so here it is:</p>
  
  <p>In an AngularJS (1.2.14) controller, I have an event listener that executes an ajax call to fetch some data when an event ('fetchData') is heard. After the fetch is successful, an event (called 'fetchSuccess') is broadcasted, that a directive is listening for (though that part is irrelevant). </p>
  
  <pre><code>$scope.$on('fetchData', function () {
              $scope
                  .submitSearch()
                  .then(function (results) {
                      $scope.$broadcast('fetchSuccess');
                  }, function () {
                      $scope.$broadcast('fetchError');
                  });
          });
  </code></pre>
  
  <p>So, in my test I want to do something like this (assume that 'this.scope' is a new $scope object on the controller in the test suite):</p>
  
  <pre><code>it('should broadcast "fetchSuccess"', inject(function ($rootScope) {
          var scope = this.scope,
              spy = chai.spy(scope, '$broadcast');
  
          // trigger the $broadcast event that calls the fetch method
          scope.$broadcast('fetchData');
  
          $rootScope.$apply();
  
          expect(scope.$broadcast).to.be.called.with('fetchSuccess');
      }));
  </code></pre>
  
  <p>But I am not clear on how to listen for the $broadcast event in an assertion. I keep getting this error: <strong>AssertionError: expected function (name, args) {...}</strong></p>
  
  <p>Just to be clear, my issue is not with the functionality of the event broadcaster or the listeners during runtime; the application works as expected. The problem is with listening to the events in the test suite.</p>
  
  <p>Note that the above code is just the necessary snippet that is needed for this question. In my application, there are other variables/methods that get set/called and those things test out correctly. Meaning that if I test to see if the actual fetching method gets called, or if a particular variable is being set appropriately, those tests pass.</p>
  
  <p>I have tried mixing and matching the scope variables and even listening for the $broadcast via <code>scope.$on('fetchSuccess', fn)</code> but nothing seems to work. The <em>fetchSuccess</em> event doesn't seem to get emitted, or I'm not listening for it properly.</p>
  
  <p>Thanks in advance,
  Ryan.</p>