Angular Karma Test getting “TypeError: 'undefined' is not a function”

11,316

You have the wrong syntax for Jasmine 2.0 - it should be

mockService.getDashboardData.and.returnValue

not

mockService.getDashboardData.andReturn

http://jsfiddle.net/xqa41at0/

Share:
11,316

Related videos on Youtube

smsh
Author by

smsh

Updated on May 26, 2022

Comments

  • smsh
    smsh almost 2 years

    Controller

    dashboardApp.controller('ExecutiveController', ['$scope',   'ExecutiveService',
    function ($scope, executiveService) {
    
        executiveService.getDashboardData().then(
        function success(response) {
            $scope.productsInfo = response.data;
        }, function error(reason) {
            console.log(reason);
        });
    
    } ]);
    

    Test case for Controller 'use strict';

    describe('ExecutiveController', function () {
    var scope, ctrl, mockService, $timeout;
    
    beforeEach(module('do.dashboard'));
    
    beforeEach(inject(function ($rootScope, $controller, $q, _$timeout_) {
    
        mockService = jasmine.createSpyObj('ExecutiveService', ['getDashboardData']);
        scope = $rootScope.$new();
        $timeout = _$timeout_;
        mockService.getDashboardData.andReturn($q.when(
    
            [{Products:{Name:'Resource ',measure:'59',Work:'60'}}]
    
        ));
    
        ctrl = $controller('ExecutiveController', {
            $scope: scope,
            ExecutiveService: mockService,
    
        });
    }));
    
    
    it('controller should not be null', function () {
        expect(ctrl).not.toBe(null);
    });
    
    
    it('should call the service method and set the scope data', function () {
        expect(mockService.getDashboardData).toHaveBeenCalled();
        $timeout.flush();
        expect(scope.productsInfo.Products.Name).toEqual('Resource');
        expect(scope.productsInfo.products.measure).toEqual(59);
    
    
    });
    
    });
    

    Here when i run it in karma runner it shows error as Angular Karma Test getting “TypeError: 'undefined' is not a function” and it gives the message which is present in the it method

    • james
      james about 9 years
      Which it function throws the error? Can you add the actual error message?
    • james
      james about 9 years
      Also which version of jasmine are you using? The syntax changed in 2.0, so you should be using mockService.getDashboardData.and.returnValue not mockService.getDashboardData.andReturn
    • smsh
      smsh about 9 years
      Actually every its methodshows error
    • smsh
      smsh about 9 years
      Chrome 40.0.2214 (Windows 7) ExecutiveController controller should not be null F AILED TypeError: undefined is not a function at Object.<anonymous> (C:/EngineeringExcellence_New/test/unit/Execut iveControllerSpec.js:13:38) at Object.e [as invoke] (C:/EngineeringExcellence_New/app/js/vendor/ angular.min.js:37:19) at Object.workFn (C:/EngineeringExcellence_New/test/lib/angular/angu lar-mock.js:2341:34) Error: Declaration Location
    • smsh
      smsh about 9 years
      ExecutiveController controller should not be null FAILED TypeError: undefined is not a function at Object.<anonymous> (C:/EngineeringExcellence_New/test/unit/ExecutiveControllerS‌​pec.js:13:42)at Object.e [as invoke(C:/EngineeringExcellence_New/app/js/vendor/ angular.min.js:37:19) at Object.workFn (C:/EngineeringExcellence_New/test/lib/angular/angu lar-mock.js:2341:34) Error: Declaration Location at window.inject.angular.mock.inject (C:/EngineeringExcellence_New/test/lib/angular/angular-mock.‌​js:2312:33)at Suite.<anonymous>
    • james
      james about 9 years
      You need to thoroughly check your test code first. There are several typos that are causing issues.
    • Kingalione
      Kingalione about 9 years
      Can you show your conf.js ? I had the same errors and found out that I had to add some js files in the conf to bring it to work
    • smsh
      smsh about 9 years
      module.exports = function(config) { config.set({basePath: '', frameworks: ['jasmine'], files: [ 'app/js/vendor/angular.min.js', 'app/js/vendor/angular-.js', 'test/lib/angular/angular-mock.js', 'test/lib/.js', 'app/js/vendor/underscore-min.js', 'app/executive-summary/app.js', 'app/executive-summary/*.js', 'test/unit/*.js' ], exclude: [ ], preprocessors: { }, reporters: ['progress'], port: 9876, colors: true, autoWatch: true, singleRun: false }); };
    • Kingalione
      Kingalione about 9 years
      please edit your post and add the config.js there and not in a comment
    • smsh
      smsh about 9 years
      its not getting added for now error in submit so will update it later
  • Taryn
    Taryn about 9 years
    Comments are not for extended discussion; this conversation has been moved to chat.
  • james
    james about 9 years
    @bluefeet The old site used to prompt and give a link to chat. Is that no longer a feature? I would have done earlier, but i'm not sure how you explicitly move from an issue to chat?
  • Taryn
    Taryn about 9 years
    You might not have received a notification because the user technically doesn't have enough rep to chat. But if a moderator moves the comments, then the user can join.
  • smsh
    smsh over 5 years
    Thanks for the help