gpt4 book ai didi

javascript - 在同一 Controller 内测试 Angular modalInstance

转载 作者:行者123 更新时间:2023-12-03 06:42:27 26 4
gpt4 key购买 nike

我正在使用 ui-bootstrap 模态窗口,并且我正在尝试测试一种触发该模态窗口的方法。我的 Controller :

app.controller('AddProductController', ['$scope', 'ProductsService', '$uibModal', function ($scope, ProductsService, $uibModal) {
$scope.product = {};
$scope.searchCategories = function () {
ProductsService.getRootCategories().then(function (data) {
$scope.categories = data.data;
});
$scope.modalInstance = $uibModal.open({
animation: $scope.animationsEnabled,
templateUrl: 'categoryContent.html',
controller: 'AddProductController',
scope: $scope
});
$scope.modalInstance.result.then(function (category) {
$scope.searchCategory = null;
$scope.product.category = category;
}, function () {
});
};
$scope.ok = function(){
$scope.modalInstance.close($scope.product.category);
};
$scope.cancel = function(){
$scope.modalInstance.dismiss();
}]);

我的测试:

describe("Products Controller", function () {
beforeEach(function () {
module('productsController');
});

beforeEach(function () {
var ProductsService, createController, scope, rootScope,

module(function ($provide) {
$provide.value('ProductsService', {
getRootCategories: function () {
return {
then: function (callback) {
return callback({data: {name: 'category1'}});
}
};
},
});

$provide.value('$uibModal', {
open : function(){
return {
then: function (callback) {
return callback({data: {name: 'category1'}});
}
};
}
});

return null;
});
});
describe('AddProductController', function () {
beforeEach(function () {
inject(function ($controller, _$rootScope_, _ProductsService_) {
rootScope = _$rootScope_;
scope = _$rootScope_.$new();
ProductsService = _ProductsService_;
createController = function () {
return $controller("AddProductController", {
$scope: scope,
});
};
});
});
it('calling searchCategories should make $scope.categories to be defined', function () {

createController();
expect(scope.categories).not.toBeDefined();
scope.searchCategories();
expect(scope.categories).toBeDefined();
});
});

});

我的所有测试都通过了,除了这个,我得到 TypeError: $scope.modalInstance.result is undefined。有什么线索吗?

最佳答案

您似乎没有在模拟模式中定义结果。尝试这样的事情:

$provide.value('$uibModal', {
open: function () {
return {
result : {
then: function (callback) {
return callback({ data: { name: 'category1' } });
}
}
};
}
});

关于javascript - 在同一 Controller 内测试 Angular modalInstance,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37895722/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com