Ad
AngularJS Jasmine Test: TypeError: 'undefined' Is Not An Object
New to Angular and following up from my earlier post from angularjs jasmine tests: Variable vm not found I am having a TypeError in my angular tests and not sure what the problem is. Here is my test:
(function(){
'use strict';
describe('Testing DeliveriesController', function() {
beforeEach(module('app.deliveries'));
describe('Testing deliveries controller', function(){
var vm, controller;
beforeEach(inject(function($controller, $rootScope){
vm = $rootScope.$new();
controller = $controller('DeliveriesController', {$scope:vm});
}));
afterEach(function() {
vm = undefined;
controller = undefined;
});
describe('priorities length', function(){
it('it should test priority length', function () {
expect(vm.priorities.length).toBe(0);
});
});
});
});
})();
The error I get is as follows:
PhantomJS 1.9.8 (Mac OS X 0.0.0) Testing DeliveriesController Testing deliveries controller priorities length it should test priority length FAILED
Error: [$injector:unpr] Unknown provider: DeliveriesServiceProvider <- DeliveriesService <- DeliveriesController
http://errors.angularjs.org/1.3.20/$injector/unpr?p0=DeliveriesServiceProvider%20%3C-%20DeliveriesService%20%3C-%20DeliveriesController
at /Users/rgoti/ingestion/external-ingestion/app/public/bower_components/angular/angular.js:4031
at getService (/Users/rgoti/ingestion/external-ingestion/app/public/bower_components/angular/angular.js:4178)
at /Users/rgoti/ingestion/external-ingestion/app/public/bower_components/angular/angular.js:4036
at getService (/Users/rgoti/ingestion/external-ingestion/app/public/bower_components/angular/angular.js:4178)
at invoke (/Users/rgoti/ingestion/external-ingestion/app/public/bower_components/angular/angular.js:4210)
at instantiate (/Users/rgoti/ingestion/external-ingestion/app/public/bower_components/angular/angular.js:4227)
at /Users/rgoti/ingestion/external-ingestion/app/public/bower_components/angular/angular.js:8524
at /Users/rgoti/ingestion/external-ingestion/app/public/bower_components/angular-mocks/angular-mocks.js:1916
at /Users/rgoti/ingestion/external-ingestion/app/public/src/app/deliveries/deliveries.spec.js:12
at invoke (/Users/rgoti/ingestion/external-ingestion/app/public/bower_components/angular/angular.js:4219)
at workFn (/Users/rgoti/ingestion/external-ingestion/app/public/bower_components/angular-mocks/angular-mocks.js:2475)
undefined
TypeError: 'undefined' is not an object (evaluating 'vm.priorities.length')
at /Users/rgoti/ingestion/external-ingestion/app/public/src/app/deliveries/deliveries.spec.js:23
Ad
Answer
Courtesy: comment from @StubbbOrn:
Could you show controller's code? It would help to understand problem better. Looks like DeliveriesController depends not only on $scope but also on DeliveriesService. When you instantiate controller you should provide it all dependencies (either real or mocked ones).
This solution worked for me and was the solution. Thanks @StubbOrn
Ad
source: stackoverflow.com
Related Questions
- → How to update data attribute on Ajax complete
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → Octobercms Component Unique id (Twig & Javascript)
- → Passing a JS var from AJAX response to Twig
- → Laravel {!! Form::open() !!} doesn't work within AngularJS
- → DropzoneJS & Laravel - Output form validation errors
- → Import statement and Babel
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM
Ad