Search code examples
angularjsangularjs-ngmock

$httpBackend .whenGet is not a function in Angular


I am trying to make a dummy service to get data in Angular, I am using ngMockE2E, and my code for the Mock looks like this:

(function () {
    "use strict"
    var app = angular
        .module("productResourceMock", ["ngMockE2E"]);

    app.run(function ($httpBackend) {
       var products = [
           {
               "productId": 1,
               "productName": "mobile1",
               "productCode": "heh4",
               "releaseDate": "May 21, 2013",
               "description": "very nice mobile",
               "cost": 200,
               "price": 300,
               "category": "Electronics",
               "tags": ["mobile", "electronic"],
               "imageUrl": "images/img1.jpg"
           },
           {
               "productId": 2,
               "productName": "mobile2",
               "productCode": "heh4",
               "releaseDate": "May 21, 2012",
               "description": "not a nice mobile",
               "cost": 100,
               "price": 500,
               "category": "Electronics",
               "tags": ["mobile", "Electronic"],
               "imageUrl": "images/img2.jpg"
           }];

        var productUrl = "/api/products";
        $httpBackend.whenGet(productUrl).respond(products);
    });

}());

I have defined my controller, and inside it, it has this code:

(function () {
    "use strict"
    angular
        .module("productManagement")
        .controller("ProductListCtrl",
                    ["productResource",
                        ProductListCtrl]);

    function ProductListCtrl(productResource) {
        var vm = this;
        productResource.query(function(data){
         vm.products = data;
        });
    }
}());

And for my service that sends the REST requests, I have this code:

(function () {
    "use strict"
    angular
        .module("common.services")
        .factory("productResource",
                ["$resource", productResource]);


    function productResource($resource) {
        return $resource("/api/products/:productId");
    }
}());

am still getting this error: Uncaught TypeError: $httpBackend.whenGet is not a function.

Any help is appreciated, or any clarification needed, please let me know.


Solution

  • Answer is simple: replace whenGet with whenGET

    Be careful to write the http verb all in uppercase.

    See ngMock documentation