Service is not defined while testing for angular service using karma/jasmine

2,335 views
Skip to first unread message

Salim Malik

unread,
May 23, 2015, 4:35:20 PM5/23/15
to jasmi...@googlegroups.com
I have written a simple  test to determine whether a service call returns a promise . But all i get a service undefined error and I cannot seems to figure out the rootcause for this after spending hours on this . I am new to jasmine . Appreciate if anyone could help me out to isolate the cause for this issue

'use strict';

describe('CompanyService', function () {

    var $httpBackend, companyService;

    beforeEach(module('MyCompany'));

    beforeEach(inject(function (_$httpBackend_, _companyService_) {
        $httpBackend = _$httpBackend_;
        companyService = _companyService_;
    }));

    it('should return a promise for getCompany function', function () {
        expect(typeof companyService.getCompany('foobar').then).toBe('function');
    });
});

Following is the companyservice

angular.module('MyCompany').factory('CompanyService',
    function($http, MyCompanyApiProvider, $upload) {
        'use strict';

        var _company = null;

        function getCompany(companyId) {
            var x = $http.get(MyCompanyApiProvider.url('companies/' + companyId));
            return x;
        }

        return {
            getCompany: getCompany
        };
    }
);
My Karma.conf.js file
module.exports = function(config) {
  config.set({

    // base path that will be used to resolve all patterns (eg. files, exclude)
    basePath: '',


    // frameworks to use
    // available frameworks: https://npmjs.org/browse/keyword/karma-adapter
    frameworks: ['jasmine'],


    // list of files / patterns to load in the browser
    files: [
            'node_modules/requirejs/require.js',
            'bower_components/angular/angular.js',
            'node_modules/angular-mocks/angular-mocks.js',
            'bower_components/ng-file-upload/**/*.js',
            'bower_components/angular-ui-router/release/**/*.js',
            'bower_components/angular-bootstrap/**/*.js',
            'bower_components/angular-translate/**/*.js',
            'bower_components/angular-translate-loader-static-files/**/*.js',
            'bower_components/angular-pnotify/src/**/*.js',
            'bower_components/angular-local-storage/**/*.js',
            'bower_components/angular-loading-bar/build/**/*.js',
            'app/app.js',
            'app/company/CompanyService.js',
            'test/**/*Spec.js'


    ],


    // list of files to exclude
    exclude: [
    ],


    // preprocess matching files before serving them to the browser
    // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
    preprocessors: {
    },


    // test results reporter to use
    // possible values: 'dots', 'progress'
    // available reporters: https://npmjs.org/browse/keyword/karma-reporter
    reporters: ['progress'],


     // web server port
        hostname: 'localhost',
        port: 44455,


    // enable / disable colors in the output (reporters and logs)
    colors: true,


    // level of logging
    // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
    logLevel: config.LOG_INFO,


    // enable / disable watching file and executing tests whenever any file changes
    autoWatch: true,


    // start these browsers
    // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
    browsers: ['Firefox'],


    // Continuous Integration mode
    // if true, Karma captures browsers, runs the tests and exits
    singleRun: true
  });
};



Folder structure

Folder structure : 
    |WebApiRole/Karma.Conf.js
    |WebApiRole/app/app.js
    |WebApiRole/app/company/CompanyService.js
    |WebApiRole/test/company/CompanyServiceSpec.js

Error stacktrace

Firefox 38.0.0 (Windows 8.1) companyService should return a promise for getCompany function FAILED
    minErr/<@C:/WebApiRole/bower_components/angular/angular.js:63:12
    loadModules/<@C:/WebApiRole/bower_components/angular/angular.js:4138:15
    forEach@C:/WebApiRole/bower_components/angular/angular.js:323:11
    loadModules@C:/bower_components/angular/angular.js:4099:5
    createInjector@C:/bower_components/angular/angular.js:4025:11
    workFn@C:/WebApiRole/node_modules/angular-mocks/angular-mocks.js:2409:44
    TypeError: companyService is undefined in
    C:/WebApiRole/test/company/CompanyServiceSpec.js (line 15)
    @C:/WebApiRole/test/company/CompanyServiceSpec.js:15:16
    Firefox 38.0.0 (Windows 8.1): Executed 1 of 1 (1 FAILED) ERROR (0.035 secs / 0.015 secs)
Thank you in advance !

Salim Malik

unread,
May 26, 2015, 12:30:32 PM5/26/15
to jasmi...@googlegroups.com

I am trying run following test case using karma.

'use strict';

describe('CompanyService', function () {
var $httpBackend, companyService; beforeEach(module('MyCompany')); beforeEach(inject(function (_$httpBackend_, _companyService_) { $httpBackend = _$httpBackend_; companyService = _companyService_; })); it('should return a promise for getCompany function', function () { expect(typeof companyService.getCompany('foobar').then).toBe('function'); }); });

Following is the Companyservice.js

angular.module('MyCompany').factory('CompanyService',
    function($http, MyCompanyApiProvider, $upload) {
        'use strict';

        var _company = null;

        function getCompany(companyId) {
            var x = $http.get(MyCompanyApiProvider.url('companies/' + companyId));
            return x;
        }

        return {
            getCompany: getCompany
        };
    }
);

// Karma configuration


 Folder structure : 
    |WebApiRole/Karma.Conf.js
    |WebApiRole/app/app.js
    |WebApiRole/app/company/CompanyService.js
    |WebApiRole/test/company/CompanyServiceSpec.js

I get an error saying companyService is undefined just before calling getcompany method . what am i missing here ?

Ben Loveridge

unread,
May 26, 2015, 5:43:38 PM5/26/15
to jasmi...@googlegroups.com
In your test, you are requiring _companyService_ (lowercase c) but in your source you are defining it as CompanyService (capital C). Have you tried making the casing match?

- Ben Loveridge


--
You received this message because you are subscribed to the Google Groups "Jasmine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jasmine-js+...@googlegroups.com.
To post to this group, send email to jasmi...@googlegroups.com.
Visit this group at http://groups.google.com/group/jasmine-js.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages