Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$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.

like image 721
David Hemsey Avatar asked Oct 04 '15 12:10

David Hemsey


People also ask

What is $httpbackend in ngmock?

- service in module ngMock Overview Fake HTTP backend implementation suitable for unit testing applications that use the $http service. Note: For fake HTTP backend implementation suitable for end-to-end testing or backend-less development please see e2e $httpBackend mock.

How to make HTTP-request from angular service?

To make HTTP-request from our service, we need the angular HttpClient. We can easily request this client via dependency injection. To make a request, we create a new method in our service. These methods are typically named after their HTTP verb (get, post, put, etc.), but using get set or remove does work, as well.

How to import httpclient in Angular 2?

Let's get started! First of all, we need to import the HttpClient-module into the parent module. If you have just started a new angular project, that would be the AppModule. To import the module, just add it to the import section of the parent module.

What is the default HTTP response format in angular?

HTTP supports a huge range of different options, parameters, headers and formats. To make all of these different requests from the HttpClient, all of its methods take an optional options object. As of angular 4.3, the default response format is JSON. That makes the usage of the HttpClient very easy.


1 Answers

Answer is simple: replace whenGet with whenGET

Be careful to write the http verb all in uppercase.

See ngMock documentation

like image 185
Michael P. Bazos Avatar answered Sep 21 '22 15:09

Michael P. Bazos