15

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":"bob@bobs.com"}, {"id":2,"name":"bob2","email":"bob2@bobs.com"}]

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

isherwood
  • 58,414
  • 16
  • 114
  • 157
ses
  • 13,174
  • 31
  • 123
  • 226

2 Answers2

9

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.

artur grzesiak
  • 20,230
  • 5
  • 46
  • 56
7

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.

ses
  • 13,174
  • 31
  • 123
  • 226
  • Hi, I'm currently experiencing the same exception. I see you solved it with the `passThrough()` function. But do you know why it is loading/GETting the `user-list.html page` ? In your test you mock the `$httpBackend.whenGET` so why the loading of the html file? – Michel Mar 29 '16 at 20:11
  • As far as I remember it was loading the page because I did mistake in mocked URL, so instead of hitting mocked URL hit real one - do it loaded the real page. Tried. – ses Mar 29 '16 at 22:58