0

I have an angular app using AngularAMD with require.js. I want to mock a service like here:

module(function ($provide) {
    $provide.service('ToolsService', function () {
        var toolsServiceMock = {
          someMethod: function () { }
        };
        return toolsServiceMock;
    });
});

And inject it like:

angularAMD.inject(function ($injector) {
    toolsService = $injector.get('ToolsService');
});

But $injector gives me real service instead of my mock. Also the function($provide) is never called.

When I change angularAMD.inject() to angular inject() I get the mock, but other application components don't work.

How can I tell angularAMD to use mocks instead of real implementations?

glepretre
  • 8,154
  • 5
  • 43
  • 57
Michal Dymel
  • 4,312
  • 3
  • 23
  • 32
  • don't you need to provide your mock service on the test? `spyOn($injector, 'get').andReturn(mockToolService)` – Raulucco Jun 24 '15 at 13:19

1 Answers1

0

You can do the following to test the service with angularAMD.

define(['app','angularAMD'],function(app,angularAMD){
    'use-strict';
    describe('ToolsService',function(){
        // load the controller's module
        beforeEach(angular.mock.module('appModule'));
        var moduleToInject;
        // Initialize the controller and a mock scope
        beforeEach(function(){
            angularAMD.inject(function ('ToolsService') {

            });
        }); 
        it('should check all methods', function() {

            //code to test


        });
    });
});
ngLover
  • 4,439
  • 3
  • 20
  • 42