タイムアウトをテストしようとしていますが、要素の範囲に問題があります。私はJSテストを書くのが初めてです。AngularJSジャスミンテスト - 要素分離スコープが定義されていません
it('should have timer defined at start')
のテストでは、my変数は未定義として記録されます。何故ですか?
私はisolateScope()がディレクティブのスコープを引き出すと思っていましたか?
私のテストは、次のようになります。
app.directive('timeout', function(timeoutService){
return {
restrict: "A",
scope: {
onWarning: "&", // what function to fire when showing a warning
},
link: function(scope, elem, attr){
scope.timeoutService = timeoutService;
if(!scope.onWarning){
throw new Error("Must provide on-warning for timeout directive.");
}
//console.log(scope.onWarning, scope.onTimeout);
// register timeouts and warnings with the service
timeoutService.onWarning = scope.onWarning;
}
}
});
app.service('timeoutService', function($timeout){
var _this = this;
var timer = null;
var time = 5000;
this.startTimer = function(){
timer = $timeout(function(){
if(_this.onWarning){
_this.onWarning()();
}
}, time)
}
this.startTimer();
})
たぶん私は間違ってテストしてる?私のディレクティブは、このようになります
https://plnkr.co/edit/IMrPd9g6HFSkgHizFF9p?p=preview
describe('Testing timeout', function(){
var scope, $timeout, element;
beforeEach(inject(function ($rootScope, $compile, _$timeout_, $injector){
scope = $rootScope.$new();
$timeout = _$timeout_;
scope.onWarning = function(){
scope.warned = true;
};
element = '<div timeout on-warning="onWarning" on-timeout="onTimeout"></div>';
element = $compile(element)(scope);
scope.$apply();
scope.$digest();
}));
it('should have timer defined at start', function(){
console.log(element.isolateScope() , scope)
expect(element.isolateScope().timeoutService.timer).not.toBeFalsy;
});
it('should have run warning function', function(){
$timeout.flush(5000);
expect(scope.warned).toBe(true);
});
});
私が作成した例が少し簡略化されているのに、ドキュメントのどこにでも$ timerサービスが表示されません – SoluableNonagon
このディレクティブの目的は、一定量の後にdivのユーザに警告メッセージを表示することですアイドル時間のどの警告機能の目的です – SoluableNonagon
申し訳ありませんが、誤植。 $ timeoutは私が意味するものです。あなたはすでにそれを使用しています。既存のサービスを包むラッパーだけのサービスを作成する必要はなく、既存のタイムアウトを追跡することも同じですが、Googleよりも経験の少ない人が徹底的にテストして書いた方法ではありませんチーム。 –