2016-05-18 7 views
2

私の分度器テストで、私のアプリケーションでエラーが発生したときに表示される警告をテストしたいと思います。分断器のテスト - dismiss-on-timeout属性を持つui-bootstrap-alert

<uib-alert type="danger" dismiss-on-timeout="5000" close="closeAlert()"> 
    <span>Error Message</span> 
</uib-alert> 

テストコードは次のようになります:

... 
element(by.css('.alert-danger')).getText().then(function(text) { 
    expect(text).to.equal('Error Message') 
}) 
... 

分度器を完了するために、5000ミリ秒のタイムアウトを待ち、テストがエラーメッセージで失敗します。

NoSuchElementError: No element found using locator: By(css selector, .alert-danger) 
HTMLは次のようです

dismiss-on-timeout属性を削除すると、テストに合格します。私は

browser.ignoreSynchronization = true 

を設定した場合、分度器はタイムアウトを待ちませんが、テストは同じメッセージで失敗します。

私は(分度器が$intervalを待たないので、そのソリューションが動作します)というよりも、$timeout

$intervalを使用するようにUibAlertControllerを変更することによってこの問題を解決することができ、よりよい解決策はありますか? - 私はフォークする必要はありませんangular-ui-bootstrap

答えて

0

分度器が$ timeoutをどのように処理するかについて、進行中の議論がありました。this号を分度器レポで参照する角度uiブートストラップレポのthis号を参照してください。

1

これは、Rob J氏によれば、これは、Angular ui-bootstrapではなく、分度器の問題です。

それはここで、誰にも便利だ場合は、UibAlertControllerの私のバージョンは次のとおりです。もちろん

.controller('UibAlertController', ['$scope', '$attrs', '$interpolate', '$interval', function($scope, $attrs, $interpolate, $interval) { 
    $scope.closeable = !!$attrs.close; 

    var dismissOnTimeout = angular.isDefined($attrs.dismissOnTimeout) ? $interpolate($attrs.dismissOnTimeout)($scope.$parent) : null; 

    if (dismissOnTimeout) { 
    var alertInterval = $interval(function() { 
     $scope.close(); 
     $interval.cancel(alertInterval); 
    }, parseInt(dismissOnTimeout, 10)); 
    } 
}]) 

、あなたはまた、ユニットテストalert.spec.jsをリファクタリングする必要があります

describe('uib-alert', function() { 
    var element, scope, $compile, $templateCache, $interval; 

    beforeEach(module('ui.bootstrap.alert')); 
    beforeEach(module('uib/template/alert/alert.html')); 

    beforeEach(inject(function($rootScope, _$compile_, _$templateCache_, _$interval_) { 
    scope = $rootScope; 
    $compile = _$compile_; 
    $templateCache = _$templateCache_; 
    $interval = _$interval_; 

... 

    it('should close automatically if dismiss-on-timeout is defined on the element', function() { 
    scope.removeAlert = jasmine.createSpy(); 
    $compile('<uib-alert close="removeAlert()" dismiss-on-timeout="500">Default alert!</uib-alert>')(scope); 
    scope.$digest(); 

    $interval.flush(1000); 
    expect(scope.removeAlert).toHaveBeenCalled(); 
    }); 

    it('should not close immediately with a dynamic dismiss-on-timeout', function() { 
    scope.removeAlert = jasmine.createSpy(); 
    scope.dismissTime = 500; 
    $compile('<uib-alert close="removeAlert()" dismiss-on-timeout="{{dismissTime}}">Default alert!</uib-alert>')(scope); 
    scope.$digest(); 

    $interval.flush(100); 
    expect(scope.removeAlert).not.toHaveBeenCalled(); 

    $interval.flush(500); 
    expect(scope.removeAlert).toHaveBeenCalled(); 
    }); 
}); 
関連する問題