1

I am trying to set up a unit test for AngularJS controller which includes a service that calls $http.get method. However, even though I use $httpBackend.expectGET before $httpBackend.flush(), app is trying to GET template HTML resources, so it gives an unexpected request error.

beforeEach(inject(function($injector){
        controller = $injector.get('$controller');
        scope = $injector.get('$rootScope').$new();
        httpBackend = $injector.get('$httpBackend');
        httpBackend.whenGET(UrlPrefix+"/promotions/default.json").respond("yeh");

    }));

 it("should fetch promotions", function() {

        httpBackend.expectGET(UrlPrefix+"/promotions/default.json") ;


        Controller = controller('promotionsController',{

            $scope : scope

        });

        scope.getPromotions();
        httpBackend.flush();
    });
});

I am getting:

Unexpected request : GET ./template/loader/app-loader.html

This is my template that should be loaded before the controller. Do you have any idea how to bypass template GETS?

Davin Tryon
  • 66,517
  • 15
  • 143
  • 132
Özüm
  • 55
  • 1
  • 3

2 Answers2

0

If having a real HTML template is not important for this specific test, you can prevent it from being downloaded by populating $templateCache with some dummy template. Something like this

beforeEach(inject(function($injector, $templateCache) {
    // ...
    $templateCache.put('./template/loader/app-loader.html', '');
}));

You may probably need to adjust template url to match real one being requested.

dfsq
  • 191,768
  • 25
  • 236
  • 258
  • I tried what you suggested. It still gives the same error. It felt like it would work though, it makes a lot of sense. – Özüm Sep 30 '14 at 10:22
  • Are you sure urls match? – dfsq Sep 30 '14 at 10:42
  • Well angular adds ?NoCache = 1230123819 parameter to it. So practically they are not the same. However that number is created without my control. – Özüm Sep 30 '14 at 11:22
  • Angular should not load template if the the one with corresponding URL is already is stored in cache. If it's not there then angular issues new request. `NoCache` parameter doesn't sound like Angular related though. – dfsq Sep 30 '14 at 11:40
  • You are right about the NoCache matter. It was related to the project I am working on. However the templatecaching still does not work. What I reallly do not understand it that how can $httpBackend has such a property. It makes it unusable. I dont think angularjs intends it to be this way. I ran into similar problems in stackoverflow, and those people forgot to put expectGET. In my case, it was always there and does not change anything. – Özüm Oct 01 '14 at 06:56
0

As far as I know I think you should bypass it

 $httpBackend.whenGET('./template/loader/app-loader.html').passThrough();
Shubham
  • 189
  • 9