2017-07-15 10 views
0

私は$ intervalの単位テストを書いています。コードは angular.module( 'serviceInvoker'、[])のようになります。実行ブロックにあるintervalのangularjsユニットテストを書くには

run(function($rootScope, $http, $interval, DateTimeFormatter) { 
    $rootScope.loadData = function(serviceName, dataName) { 
     $http.get(serviceName) 
      .then(function(result) { 
       serviceSuccessFunc(result, dataName) 
      }) 
      .catch(function(error) { 
       serviceErrorFunc(error, dataName) 
      }); 

     $rootScope.current = moment().toISOString(); 
     $rootScope.now = moment($rootScope.current).format(DateTimeFormatter.DayHoursFormat); 
    }; 

    $rootScope.autoRefresh = function(serviceName, dataName, interval) { 
     $interval(function() { 
      $rootScope.loadData(serviceName, dataName) 
     }, interval); 
    }; 

    var serviceSuccessFunc = function(result, dataName) { 
     $rootScope[dataName] = result.data; 
    }; 

    var serviceErrorFunc = function(error, dataName) { 
     $rootScope[dataName] = error; 
    }; 
}); 

テストコードは次のようである:

describe('serviceInvoker', function() { 

beforeEach(module('serviceInvoker')); 
beforeEach(module(function($provide) { 
    $provide.value('DateTimeFormatter', { 
     DayHoursFormat: 'HH:mm' 
    }); 

    $provide.value('serviceSuccessFunc', jasmine.createSpy()); 
    $provide.value('serviceErrorFunc', jasmine.createSpy()); 
})); 
var $interval; 
beforeEach(inject(function (_$rootScope_, _$interval_, _serviceSuccessFunc_, _serviceErrorFunc_) { 
    $rootScope = _$rootScope_; 
    scope = $rootScope.$new(); 
    $interval = _$interval_; 
    serviceSuccessFunc = _serviceSuccessFunc_; 
    serviceErrorFunc = _serviceErrorFunc_; 
})); 

describe("loadData function ", function() { 

    it("should expose loadData function to $rootScope", function() { 
     expect(angular.isFunction($rootScope.loadData)).toBe(true); 
    }); 

    it("should be called", inject(function($http) { 
     spyOn($rootScope, 'loadData'); 
     $rootScope.loadData('service', 'data'); 
     expect($rootScope.loadData).toHaveBeenCalledWith('service', 'data'); 
    })); 
}); 

describe("autoRefresh function ", function() { 

    it("should expose autoRefresh function to $rootScope", function() { 
     expect(angular.isFunction($rootScope.autoRefresh)).toBe(true); 
    }); 

    it("should be called", function() { 
     var $intervalspy = jasmine.createSpy('$interval', $interval); 
     spyOn($rootScope, 'autoRefresh').and.callThrough(); 

     $rootScope.autoRefresh('service', 'data','interval'); 
     expect($rootScope.autoRefresh).toHaveBeenCalledWith('service', 'data', 'interval'); 

     expect($intervalspy).toHaveBeenCalled(); 
     // expect($intervalspy).toHaveBeenCalledWith(jasmine.any(Function), 1000); 
    }); 
}); 

})。

しかし、間隔のエラーがあります。エラー:spy $ intervalが呼び出されました。

私は(実行ブロック内の)関数内にある区間の単位テストを書く方法を知らないのですが、誰かに助けてくれますか?

答えて

0

$intervalspyはテスト自体では使用されません。それは呼び出されません。

他のテストされているサービスと同様に、$intervalは、スパイク、模擬またはスタブする必要があります。それはthatのようなデコレータをスパイすることができ、そしてスタブはさらに簡単です:

beforeEach(() => { 
    module({ $interval: jasmine.createSpy() }) 
}); 

... 
$rootScope.autoRefresh('service', 'data', 'interval'); 
expect($interval).toHaveBeenCalledWith(jasmine.any(Function), 'interval'); 
expect($rootScope.loadData).not.toHaveBeenCalled(), 

var intervalCallback = $interval.calls.first().args[0]; 
intervalCallback(); 
expect($rootScope.loadData).toHaveBeenCalledWith(...), 
+0

はどうもありがとう、私は何であるか理解していない: beforeEach(()=> { モジュール({$間隔:jasmine.createSpy()}) }); どこで定義しますか?私はangularjsを初めて使っています。ありがとう! –

+0

他のbeforeEachブロックと同じ場所に定義する必要があります。 – estus

+0

このような? beforeEach(注入(関数(_ $ rootScope_、_ $のinterval_、_serviceSuccessFunc_、_serviceErrorFunc_){ $ rootScope = _ $のrootScope_; 範囲= $ rootScope $新しい(); $間隔= _ $のinterval_; serviceSuccessFunc = _serviceSuccessFunc_; serviceErrorFunc = _serviceErrorFunc_; $ interval = jasmine.createSpy(); })); –

関連する問題