2012-12-03 65 views
1

我有一个非常简单的控制器,获取对象的简单的JSON列表...角HTTP测试

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 
    });})); 

然而我在萤火虫窗口object undefined中收到错误。我在这里做错了什么?

+0

$ httsypBackend.expectGET('/ ProductGroup')中的'$ httsypBackend'。如果不是,那会是你的问题。 – dnc253

+0

是的,这是一个错字。我复制并粘贴到另一个编辑器中,以便我可以接受问题,因为我通常使用2个选项卡空格。 :-) –

回答

1

找到了答案。如果我删除从$ http.get方法错误回调函数,那么它的工作原理,也就是后续删除...

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

我要说的角度肯定是有人陡峭的学习曲线,谁也不是一天到一天的JavaScript程序员(虽然我似乎做得越来越多)。感谢您的帮助KatieK :-)