2017-03-14 66 views
0

我繼承了一些我嘗試工作的破碎測試。我們正在使用Angular 1.5和Jasmine。在「真實」控制器中,我們有以下幾種:

function myfunction() { 
    $scope.stop = function() { 
    $interval.cancel(promise); 
    $timeout.cancel(timeout); 
    }; 
    ......... 

該函數在控制器的頂部被調用。

在我的測試文件,我設置使用間隔和超時:

'use strict'; 

var $scope; 
var $timeout; 


describe('Pas', function(){ 
describe('PasController', function() { 
    var $this; 
    var $rootscope; 
    var PasService; 
    var $interval = jasmine.createSpy('$interval', $interval).and.callThrough(); 
    var $timeout = jasmine.createSpy('$timeout', $timeout).and.callThrough(); 

    beforeEach(module('CL.Pas')); 

    beforeEach(inject(function (_$controller_, _$rootScope_, _PasService_, _$interval_, _$timeout_) { 
     $rootscope = _$rootScope_; 
     PasService = _PasService_; 
     $interval = _$interval_; 
     $timeout = _$timeout_; 

     var data = { 
      "Id": "00000000-0000-0000-0000-000000000000", 
      "SectionSlug": "string", 
      "SectionName": "PAS", 
      "Sections": [ 
       { 
        id: "00000000-0000-0000-0000-000000000001", 
        Mandatory: true, 
        description: "test 1", 
        name: "Section 1", 
        status: "Not Started" 
       }, 
       { 
        id: "00000000-0000-0000-0000-000000000003", 
        Mandatory: false, 
        description: "test 3", 
        name: "Section 3", 
        status: "Started" 
       }, 
       { 
        id: "00000000-0000-0000-0000-000000000002", 
        Mandatory: true, 
        description: "test 2", 
        name: "Section 2", 
        status: "Not Started" 
       } 
      ], 
      Forms: [] 
     }; 

     spyOn(PasService, 'canSubmitMandatorySections').and.callFake(getFakePromise({Result: false})); 
     spyOn(PasService, 'canSubmitOptionalSections').and.callFake(getFakePromise({Result: false})); 
     spyOn($interval, 'cancel'); 
     spyOn($timeout, 'cancel'); 


     $this = _$controller_('PasController', {sections: data, PasService: PasService, canSubmitIndividual: true, $scope: $scope, $timeout : $timeout, $interval: $interval}); 
    })); 

    it('should split available sections', function() { 
     $rootscope.$apply(); 

     expect($this.sections.mandatory.length).toEqual(2); 
     expect($this.sections.optional.length).toEqual(1); 
    }); 
}); 
}); 

在我的測試運行,我收到一個類型錯誤點:無法設置屬性「停止」的定義。我如何在測試中僞造這個?試圖從$控制器

+0

你能後整個測試請設置$這個時候

在安裝它的錯誤?或者提供一個鏈接到jsbin等?那'停止'似乎不是指你寫的東西($ scope.stop)。 – rrd

+0

@rrd我改變了問題來顯示整個測試 – bilpor

回答

0

我忘了實例化範圍$scope = $rootscope.$new();

相關問題