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
Related videos on Youtube
Author by
smsh
Updated on May 26, 2022Comments
-
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 about 9 yearsWhich
it
function throws the error? Can you add the actual error message? -
james about 9 yearsAlso which version of jasmine are you using? The syntax changed in 2.0, so you should be using
mockService.getDashboardData.and.returnValue
notmockService.getDashboardData.andReturn
-
smsh about 9 yearsActually every its methodshows error
-
smsh about 9 yearsChrome 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 about 9 yearsExecutiveController controller should not be null FAILED TypeError: undefined is not a function at Object.<anonymous> (C:/EngineeringExcellence_New/test/unit/ExecutiveControllerSpec.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 about 9 yearsYou need to thoroughly check your test code first. There are several typos that are causing issues.
-
Kingalione about 9 yearsCan 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 about 9 yearsmodule.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 about 9 yearsplease edit your post and add the config.js there and not in a comment
-
smsh about 9 yearsits not getting added for now error in submit so will update it later
-
-
Taryn about 9 yearsComments are not for extended discussion; this conversation has been moved to chat.
-
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 about 9 yearsYou 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 over 5 yearsThanks for the help