2015-10-02 11 views
6

में नियंत्रक निर्देशों का परीक्षण कैसे करें कई शोधों के बाद, मैं एक कोणीय निर्देश का सही ढंग से परीक्षण करने में असमर्थ रहा हूं, क्योंकि मैं इसके नियंत्रक के अंदर कार्यों तक नहीं पहुंच सकता।AngularJS

angular.module('app'). 
    directive("accordionItem", function() { 
     return{ 
      restrict: 'E', 
      replace: true, 
      templateUrl: function (elem, attr) { 
       return 'partials/invoice/' + attr.temp + '.html'; 
      }, 
      scope: { 
       invoice: '=', 
       temp: '@' 
      }, 
      controller: function ($scope, listSelectionService, $state) { 

      $scope.selectItem = function() { 
       if ($scope.isOpen()) { 
        listSelectionService.selectedItem = -1; 
       } 
       else { 
        listSelectionService.selectedItem = $scope.invoice; 
       } 
      }; 
      $scope.isOpen = function() { 
       return listSelectionService.selectedItem === $scope.invoice; 
      }; 
      $scope.showFaturasSubscription = function() { 
       $state.go('app.ultimasFaturasSubscription', {subscriptionId: $scope.invoice.subscriptionId}); 
      }; 
     } 
    }; 
}); 

और अपने परीक्षण:

यहाँ निर्देश कोड है

describe('Invoice', function() { 
    var $scope = {}, controller, $httpBackend, $controller, form, element; 
    beforeEach(module('app')); 

    describe('Directives', function() { 
     beforeEach(inject(function($compile, $rootScope, _$httpBackend_, _$controller_) { 
      $httpBackend = _$httpBackend_; 
      $httpBackend.expect('GET', 'data/translation/?lang=pt').respond(200, []); 
      $httpBackend.when('GET', 'partials/invoice/undefined.html').respond(200, []); 
      $httpBackend.when('GET', 'partials/templates/loading.html').respond(200, []); 
      $httpBackend.when('GET', 'partials/invoice/invoiceContent.html').respond(200, []); 
      $scope = $rootScope.$new(); 
      $controller = _$controller_; 

      form = $compile("<accordion-item temp='invoiceContent'></accordion-item>")($scope); 
      $scope.$digest(); 

     })); 
     it('should submitButtonDisabled', inject(function($injector) { 
      var listSelectionService = $injector.get("listSelectionService"); 
      $scope.selectItem(); 
      expect(listSelectionService.selectedItem).toBe(-1); 
     })); 
    }); 
}); 

कई दस्तावेज मैं पढ़ा है के अनुसार, $ डाइजेस्ट() समारोह के बाद हम के लिए उपयोग किया जा सकता है निर्देश के नियंत्रक। ऐसा इसलिए नहीं हो रहा है क्योंकि यह मुझे निम्न त्रुटि देता है:

TypeError: $scope.selectItem is not a function 
at null.<anonymous> (http://localhost:8234/spec/invoice/invoiceDirectivesSpec.js:27:20) 
at Object.invoke (http://localhost:8234/src/main/webapp/vendor/angular/angular.js:4452:17) 
at workFn (http://localhost:8234/src/main/webapp/vendor/angular-mocks/angular-mocks.js:2420:20) 
at jasmine.Block.execute (http://localhost:8234/?:1164:19) 
at jasmine.Queue.next_ (http://localhost:8234/?:2196:33) 
at jasmine.Queue.start (http://localhost:8234/?:2149:10) 
at jasmine.Spec.execute (http://localhost:8234/?:2476:16) 
at jasmine.Queue.next_ (http://localhost:8234/?:2196:33) 
at jasmine.Queue.start (http://localhost:8234/?:2149:10) 
at jasmine.Suite.execute (http://localhost:8234/?:2621:16) 
Error: Declaration Location 
    at window.inject.angular.mock.inject (http://localhost:8234/src/main/webapp/vendor/angular-mocks/angular-mocks.js:2391:25) 
    at null.<anonymous> (http://localhost:8234/spec/invoice/invoiceDirectivesSpec.js:25:43) 
    at jasmine.Env.describe (http://localhost:8234/?:919:23) 
    at describe (http://localhost:8234/?:703:29) 
    at null.<anonymous> (http://localhost:8234/spec/invoice/invoiceDirectivesSpec.js:10:5) 
    at jasmine.Env.describe (http://localhost:8234/?:919:23) 
    at describe (http://localhost:8234/?:703:29) 
    at http://localhost:8234/spec/invoice/invoiceDirectivesSpec.js:5:1 

किसी भी मदद की वास्तव में सराहना की जाएगी।

उत्तर

5

मैं आमतौर पर निर्देशक नियंत्रकों का परीक्षण करता हूं जैसे मैं एक नियमित नियंत्रक करता हूं।

आपके निर्देश में, आपने निर्देश के हिस्से के रूप में नियंत्रक इनलाइन को परिभाषित किया है।

angular.module('app').controller('DirectiveController', function($scope) { ... }); 

संदर्भ निर्देश विन्यास में नियंत्रक:

एक मॉड्यूल पर नियंत्रक रजिस्टर: इसके बजाय, यह है कि आप एक नियंत्रक कि एक दृश्य के साथ प्रयोग किया जाता है के लिए होगा परिभाषित

controller: 'DirectiveController' 

नियंत्रक का परीक्षण:

यह या तो वास्तविक निर्देश परीक्षणों को प्रतिस्थापित या पूरक करेगा। निर्देश के बाहर नियंत्रक का परीक्षण करना बहुत आसान है, आपको निर्देश को तत्काल करने या डीओएम तत्वों से निपटने के बारे में चिंता करने की आवश्यकता नहीं है। अक्सर बार, यदि निर्देश के लिए टेम्पलेट काफी सरल है, तो मैं निर्देश परीक्षणों से भी परेशान नहीं हूं और केवल नियंत्रक का परीक्षण करता हूं। सरल उदाहरण:

var controller, scope; 

beforeEach(inject(function($rootScope, $controller) { 
    scope = $rootScope.$new(); 
    controller = $controller('DirectiveController', {$scope: scope}); 
})); 

describe('controller', function() { 
    it('exists', function() { 
    expect(controller).toBeDefined(); 
    expect(controller).not.toBeNull(); 
    }); 
});