title: Angular directive controller unit test using window.confirm
body: <p>I am trying to get 100% test coverage for a directive.  The directive has a controller with a function that uses the <code>window.confirm</code> method.</p>
  
  <pre><code>'use strict';
  
  (function() {
  
      angular
          .module('app')
          .directive('buttonToggle', buttonToggle);
  
      function buttonToggle() {
          var buttonToggleController = ['$scope', function($scope) {
              $scope.toggle = function() {
                  var confirmResponse = (window.confirm('Are you sure?') === true);
  
                  if(confirmResponse) {
                      $scope.on = !$scope.on;
                  }
                  return $scope.on;
              };
          }];
  
          return {
              restrict: 'E',
              templateUrl: 'client/modules/buttonToggle/buttonToggle.html',
              replace: true,
              scope: {
                  on: '='
              },
              controller: buttonToggleController
          };
      }
  })();
  </code></pre>
  
  <p>I have tested to make sure that everything is defined, but I am not able to enter the <code>if</code> statement in the controller's <code>$scope.toggle</code> method.</p>
  
  <pre><code>describe('The buttonToggle directive', function() {
  
      var $compile,
          $scope,
          btElement = '&lt;button-toggle&gt;&lt;/button-toggle&gt;',
          compiledElement,
          window,
          confirm,
          btElementPath = 'client/modules/buttonToggle/buttonToggle.html',
          btController;
  
      beforeEach(module('app'));
      beforeEach(module(btElementPath));
  
      beforeEach(inject(function(_$compile_, _$rootScope_, $templateCache, $window) {
          $compile = _$compile_;
          window = $window;
          spyOn(window, 'confirm');
          $scope = _$rootScope_.$new();
          var template = $templateCache.get(btElementPath);
          $templateCache.put(btElementPath, template);
          var element = angular.element(btElement);
          compiledElement = $compile(element)($scope);
          $scope.$digest();
          btController = element.controller('buttonToggle', {
              $window: window
          });
          scope = element.isolateScope() || element.scope();
      }));
  
      it('should be defined', function() {
          expect(compiledElement.html()).toContain('btn');
      });
  
      describe('buttonToggle controller', function() {
          it('should be defined', function() {
              expect(btController).not.toBeNull();
              expect(btController).toBeDefined();
          });
  
          describe('toggle', function() {
              it('should be defined', function() {
                  expect(scope.toggle).toBeDefined();
              });
  
              it('should confirm the confirmation dialog', function() {
                  scope.toggle();
                  expect(window.confirm).toHaveBeenCalled();
              });
          });
      });
  });
  </code></pre>
  
  <p>I am guessing it has something to do with mocking the <code>$window</code> service, but I'm not sure if I will be able to test that since it isn't declared globally.  So, is the controller's function fully "unit testable"?  If not, should I write the directive's controller in a separate file and use <code>angular.module.controller</code>?  If yes, then how am I able to test it, or what am I missing?</p>