2

I can't figure out why I keep getting this exception. The service is in one module, and the helper (unit under test) is in another. I'm trying to mock out the service (which is a dependency for the helper) and create the helper injecting that mocked service.

Thanks for any ideas!

TypeError: 'undefined' is not a function (evaluating 'jasmine.spyOn(myService, 'someData')')

Here's the test:

describe('myHelper test', function () {
var myHelper, myService;
var expectedData = 'test';

beforeEach(module('serviceModule'));
beforeEach(module('helperModule'));

    it('works', function() {

        module(function($provide) {
            $provide.service('myService', function() {
                someData = jasmine.spyOn(myService, 'someData').and.callFake(function(){
                   return 100; });
            });
        });

        var mockMyService;

        inject(function(myService){
            mockMyService = myService;
        });

        angular.module('helperModule').config(['myHelperProvider', function (myHelperProvider) {
            return myHelper(mockMyService, _);
        }]);

        expect(myHelper.someData()).toEqual(expectedData);
    });

});

1
  • Having trouble following your unit test code out of context - can you post the service/helper module code as well? In general, my suggestion would be to make a formal mock of the service rather than doing it in the unit test itself since that makes the mock portable. Commented Jun 25, 2015 at 21:43

1 Answer 1

2

If you are using spyOn then you should inject the real service inside the test and then do a spyOn.

 inject(function(myService){
            jasmine.spyOn(myService, 'someData').and.callFake(function(){
                   return 100; });
            });
            mockMyService = myService;
        });

Currently you are trying to create a mock service myService by injecting original service myService (which also has bug, because you have not done injection of original service)!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.