1

I have a fairly simple controller that gets a simple json list of objects ...

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

}

I then try to mock the request in the test class:

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

However I receive an error in the testacular window object undefined. What have I done wrong here?

2
  • Is the $httsypBackend in $httsypBackend.expectGET('/ProductGroup'). a typo? If not, that would be your problem. Commented Dec 3, 2012 at 20:15
  • Yes, that's a typo. I copied and pasted into another editor so I could get the question accepted as I use 2 tab spaces usually. :-) Commented Dec 3, 2012 at 23:21

1 Answer 1

1

Found the answer. If I remove the error callback function from the $http.get method then it works, i.e. remove the following ...

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

I have to say Angular sure is a steep learning curve for someone who is not a day to day JavaScript programmer (although I seem to be doing more and more). Thanks for the help anyway KatieK :-)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.