Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS $httpBackend - "No more request expected" error

Tags:

angularjs

It seems this is working solution that shows how to work with $httpBacked http://jsfiddle.net/EgMpe/8/

But for my case:

routes

app.config(['$routeProvider', function($routeProvider) { $routeProvider.

    when('/',  {templateUrl: 'partials/user-list.html'}).

...

faked service:

app.run(function($httpBackend) {

        var users = [{"id":1,"name":"bob","email":"[email protected]"}, {"id":2,"name":"bob2","email":"[email protected]"}]

        $httpBackend.whenGET('/rest/users').respond(function(method,url,data) {
            console.log("Getting users");
            return [200, users, {}];
        });
    });

..

real service:

services.factory('Users', function($resource){
    return $resource('/rest/users', {}, {
        get:        {method: 'GET', isArray:true}
    });
});

I have error when go to my "/" route that redirects me to user-list.html page:

Error: Unexpected request: GET partials/user-list.html No more request expected at $httpBackend .../mysite/public/angular/libs/angular-1.2.0/angular-mocks.js:1060:9)

Question1: Does httpBackend prevent doing any other http request?

I tried to use passThrough method to let http hit real server side:

$httpBackend.whenGET(/^\/mysite\//).passThrough();

But this does not help.

like image 756
ses Avatar asked Dec 06 '13 19:12

ses


2 Answers

Using $httpBackend you have to specify in advance all request you are going to perform. Maybe this short excerpt from Mastering Web Application Development with AngularJS will clarify why:

The verifyNoOutstandingExpectation method verifies that all the expected calls were made ($http methods invoked and responses flushed), while the verifyNoOutstandingRequest call makes sure that code under test didn't trigger any unexpected XHR calls. Using those two methods we can make sure that the code under the test invokes all the expected methods and only the expected ones.

like image 140
artur grzesiak Avatar answered Oct 21 '22 00:10

artur grzesiak


Ah.. Sorry I just was wrong with my RegEx:

if type this $httpBackend.whenGET(/partials/).passThrough();

Then all start working.

So, I got my lesson: don't forget to put: passThrough(); with right RegEx.

like image 31
ses Avatar answered Oct 21 '22 00:10

ses