title: Karma testing a call to a function inside a scope.function
body: <p>I have a UploadDocumentCtrl.js where i am accessing a service call to a function inside another function . This outer function is bound to the scope . My problem is i am unable to access the code statements inside this (Code block C) . I want to access the 'flag' variable and check if that is true . Can anyone point me in the correct direction or tell me what i am doing wrong here ? Thanks ..</p>
  
  <p>UploadDocumentsCtrl.js</p>
  
  <pre><code>(function () {
      'use strict';
  
      angular.module('myApp')
             .controller('UploadDocumentsCtrl', UploadDocumentsCtrl);
  
      UploadDocumentsCtrl.$inject = ['$rootScope', '$scope', '$modalInstance', '$window', 'companyService'];
  
      function UploadDocumentsCtrl($rootScope, $scope, $modalInstance, $window, companyService) {
  
          $scope.onFileSelect = onFileSelect;
          $scope.buttonDisabled = false;
  
  
  
          function onFileSelect(files) {
              //Can access the code here
              function upload(file) {
              //Can access the code here as well
                  companyService.uploadDocuments(file)
                      .success(function (data, status, headers, config) {
                     // Code block C 
                    // Cannot access any code here or the error code block below
                          $scope.flag = true;
                      })
                      .error(function (data, status, headers, config) {                        
                      });
              }
  
              files.forEach(function (file) {
                  file.progress = 0;
                  file.percent = 0;
  
                  $scope.filesToUpload.push(file);
                  upload(file);
              });            
          }
      }
  })();
  </code></pre>
  
  <p>Jasmine test case</p>
  
  <pre><code>(function () {
  "use strict";
      describe('UploadDocumentsCtrl', function () {
          var scope, companyService,companyControllerFactory;
  
          beforeEach(angular.mock.module('myApp'));
  
          beforeEach(inject(function($rootScope, $controller , _companyService_) {
                  companyService = _companyService_;
                  scope = $rootScope.$new();             
                  companyControllerFactory = function(){$controller('UploadDocumentsCtrl',
                              {
                                  $scope: scope,
                                  companyService: _companyService_
                              });
              };
          }));
  
          describe("onFileSelect", function() {
  
              it(" Should make the flag to true ", function() {
                  var files = [{}];
                  companyControllerFactory();
                  spyOn(companyService, 'uploadDocuments').and.returnValue({ success: function(){}});
                  scope.onFileSelect(files);
                  expect(scope.flag).toBe(true);
              });
          });
      });
  })();
  </code></pre>
  
  <p>The error i am getting while trying to do the above..</p>
  
  <blockquote>
    <p>1) Should make the flag to true
         UploadDocumentsCtrl onFileSelect
         TypeError: companyService.uploadDocuments(...).success(...) is undefined in <a href="http://localhost:9876/absoluteC:/Users" rel="noreferrer">http://localhost:9876/absoluteC:/Users</a>
         /Documents/fle/Fle/WebApiRole/app/company/UploadDocumentsCtrl.js?f11d5dcacbf2ca1d63778bfa04c582862e325523
    ( line 31)
    upload@<a href="http://localhost:9876/absoluteC:/Users/Documents/fle/Fle/WebApiRole/app/company/UploadDocumentsCtrl" rel="noreferrer">http://localhost:9876/absoluteC:/Users/Documents/fle/Fle/WebApiRole/app/company/UploadDocumentsCtrl</a>
    .js?f11d5dcacbf2ca1d63778bfa04c582862e325523:31:17
    onFileSelect/&lt;@<a href="http://localhost:9876/absoluteC:/Users/Documents/fle/Fle/WebApiRole/app/company/UploadDocum" rel="noreferrer">http://localhost:9876/absoluteC:/Users/Documents/fle/Fle/WebApiRole/app/company/UploadDocum</a>
    entsCtrl.js?f11d5dcacbf2ca1d63778bfa04c582862e325523:51:17
    onFileSelect@<a href="http://localhost:9876/absoluteC:/Users/Documents/fle/Fle/WebApiRole/app/company/UploadDocumen" rel="noreferrer">http://localhost:9876/absoluteC:/Users/Documents/fle/Fle/WebApiRole/app/company/UploadDocumen</a>
    tsCtrl.js?f11d5dcacbf2ca1d63778bfa04c582862e325523:46:13
    @<a href="http://localhost:9876/base/test/company/UploadDocumentsCtrlSpec.js?c5db561e203bdfae1a6f7509347d3f7032e8f785:35:17" rel="noreferrer">http://localhost:9876/base/test/company/UploadDocumentsCtrlSpec.js?c5db561e203bdfae1a6f7509347d3f7032e8f785:35:17</a></p>
  </blockquote>