No expected requests with angular-mock, $resource and ionic


#1

Heya, I’m having trouble getting angular-mocks in tests to work correctly. I am testing a little service:

angular.module('bar')
    .factory('FooService', function($resource) {
        return {
            getFoo: function() {
                var fooResource = $resource('http://localhost:8081/foos', {}, {
                    get: {method: 'GET'}
                });

                return fooResource.get();
            }
        };
    });

and would expect the following test

describe('Foo Service', function () {
    beforeEach(injectDependencies('FooService'));

    it('makes a request', function () {
        this.FooService.getFoo().$promise.then(function (result) {
            console.log('result: ', result);
        });
    });
});

to return me an error message like this:

Error: Unexpected request: GET http://localhost:8081/foos

But unfortunately I get nothing and the test would not tell me that there was a request. However, when I add an $httpBackend.flush() after the promise success function it will give me the unexpected request error that I want. Of course this is not the behavior I want and am used from angular.

I am using ionic 1.1.0 with angular 1.4.3.

karma.conf.js:

files: [
      'www/lib/ionic/js/ionic.bundle.js',
      'www/lib/angular-mocks/angular-mocks.js',
      'www/lib/angular-resource/angular-resource.js',
],

I also tried not using the ionic.bundle file and including the dependencies (making sure all dependencies except angular-ui-router are also 1.4.3) into the karma.conf with no success.

karma.conf.js:

files: [
      'www/lib/angular/angular.js',
      'www/lib/angular-animate/angular-animate.js',
      'www/lib/angular-sanitize/angular-sanitize.js',
      'www/lib/angular-mocks/angular-mocks.js',

      'www/lib/angular-resource/angular-resource.js',
      'www/lib/angular-ui-router/release/angular-ui-router.js',
      'www/lib/ionic/js/ionic.js',
      'www/lib/ionic/js/ionic-angular.js',

       ...
],

Has anybody had any success with getting angular-mocks to work properly with $http/$resource in ionic? Or does anybody know of a good example/tutorial? Thanks for any help


#2

Hmm okay, maybe I was wrong about my assumptions. I guess I was using a lot of $http calls that were triggered via ui interactions like click() that trigger digests and by that the flush gets invoked implicitly?

I just tried a plain angular example and also did not receive an unexpected request when not explicitly asking for it.