Angular Karma Test getting “TypeError: 'undefined' is not a function”
Angular Karma Test getting “TypeError: 'undefined' is not a function”
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
Jasmine 2.0 的语法错误 - 应该是
mockService.getDashboardData.and.returnValue
没有
mockService.getDashboardData.andReturn
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
Jasmine 2.0 的语法错误 - 应该是
mockService.getDashboardData.and.returnValue
没有
mockService.getDashboardData.andReturn