jeudi 25 juin 2015

'undefined' is not a function - Jasmine unit test for AngularJS

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);
    });

});

Aucun commentaire:

Enregistrer un commentaire