关于测试角度模态的问题有很多问题,但它们似乎都是关于模仿模态实例的。我实际上想要调用实现,特别是打开的promise回调。这是我的模块:
angular.module('MyApp')
.directive('widgetContainer', function() {
return {
templateUrl: '/static/templates/container.html',
controller: 'ContainerCtrl'
};
})
.controller('ContainerCtrl', ['$scope', '$modal', function($scope, $modal) {
function editWidget(widget) {
var modalInstance = $modal.open({
templateUrl: '/static/templates//modal.html',
controller: 'ModalInstanceCtrl',
scope: $scope,
size: 'lg',
backdrop: 'static'
});
modalInstance.opened.then(function() {
$scope.widgetCopy = editWidgetInit(widget);
});
modalInstance.result
.then(function() {
// some result
});
return modalInstance;
}
function editWidgetInit(widget) {
widgetCopy = setSelectedChart(widget);
// lots of other setup tasks
return widgetCopy;
}
}]);
这是测试的样子。
describe('on edit widget', function() {
it('should setup selectedChart from widget', function() {
var widget = {widget: {indicators: [{name: 'indicator'}]}};
var modalInstance = scope.editWidget(widget);
rootScope.$digest();
expect(scope.selectedChart).toBe('pie');
});
});
这会使模态实例打开,但打开的承诺永远不会实现。它通常应在创建新模态实例时触发。
我可以在没有嘲笑的情况下通过真实的电话进行测试,还是有其他更好的方法来测试它?
答案 0 :(得分:0)
在您的测试中,您可以自己解决opened
承诺。
describe('on edit widget', function() {
it('should setup selectedChart from widget', function() {
var widget = {widget: {indicators: [{name: 'indicator'}]}};
var modalInstance = scope.editWidget(widget);
modalInstance.opened.resolve();
rootScope.$digest();
expect(scope.selectedChart).toBe('pie');
});
});