gpt4 book ai didi

testing - Angular http 测试

转载 作者:行者123 更新时间:2023-11-28 20:45:15 24 4
gpt4 key购买 nike

我有一个相当简单的 Controller ,它获取一个简单的对象 json 列表 ...

function ProductGroupsCtrl($scope, $http, $routeParams, sharedService, popupService) {

$scope.list = null;
$scope.selectedItem = null;
$scope.selectedItemJsonString = '';

$scope.selectItem = function (item) {
$scope.selectedItem = item;
$scope.selectedItemJsonString = JSON.stringify(item);
//alert(JSON.stringify(item));
};

$scope.closePopup = function () {
$scope.selectedItem = null;
$scope.selectedItemJsonString = '';
};

// sharedService.prepForBroadcast($routeParams.anotherVar);

$http({
method: 'GET',
url: '/ProductGroup'
}).success(function (data) {
$scope.list = data;
}).
error(function (data) {
$scope.message = 'There was an error with the data request.';
});

}

然后我尝试在测试类中模拟请求:

var scope, ctrl, $httpBackend, sharedServiceMock = {}, popupServiceMock = {};

beforeEach(inject(function (_$httpBackend_, $rootScope, $controller) {
$httpBackend = _$httpBackend_;

$httsypBackend.expectGET('/ProductGroup').
respond([{
ProductGroupID: 5,
MenuTitle: "Promotional Products",
AlternativeText: "Coming soon - a collection of environmentally friendly Promotional Products",
OrdinalPosition: 5,
Active: false
}]);


scope = $rootScope.$new();
ctrl = $controller(ProductGroupsCtrl, {
$scope: scope,
$http: $httpBackend,
sharedService: sharedServiceMock,
popupService: popupServiceMock
});}));

但是我在 testacular 窗口 object undefined 中收到一个错误。我在这里做错了什么?

最佳答案

找到了答案。如果我从 $http.get 方法中删除错误回调函数,那么它就可以工作,即删除以下 ...

error(function (data) {
$scope.message = 'There was an error with the data request.';
}

我不得不说,对于不是日常 JavaScript 程序员的人来说,Angular 确实是一个陡峭的学习曲线(尽管我似乎做的越来越多)。无论如何,感谢您的帮助 KatieK :-)

关于testing - Angular http 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13690561/

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