lundi 5 octobre 2015

jasmine: Mocking angular service yields timeout

I am trying to test an angular service. The service is using another service and I would like to mock that one.

Let's say I have a service myService that depends on the service myOtherService. When just testing the application I get

Unknown provider: myOtherServiceProvider <- myOtherService <- myService

This is because I have not included the file specifing the service in the unit test. And I also don't want because I would like to mock this service.

I have come across this reference and have tried to with something like that:

describe('Service: myService', function() {
    var myService;

    beforeEach(function(){module('myApp');});

    beforeEach(function($provide){
        module(function($provide) {
            $provide.service('myOtherService', function() {
                this.doSomething = function(){
                    //...
                }
            });
        });
    });

    beforeEach(inject(function(_myService_) {
        myService = _myService_;
    }));

    describe('Duck Typing', function() {
        it('should contain a doSomething() API function', function(){
            expect(angular.isFunction(myService.doSomething)).toBe(true);
        });
    });
});

But when running the unit test I get the following error:

Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.

So I would like to learn what is wrong here and how I can mock the service dependency correctly.

Thanks!

Aucun commentaire:

Enregistrer un commentaire