I am attempting to do my first unit test in AngularJS and it has not been going well. I am unsuccessfully attempting to run a simple unit test example that I found on the web. After quite a few hours, I need some help.
I have put the code on Plunker. Can anyone help to get it going? https://plnkr.co/edit/D7VwLfoFp0QzhobrKN9f It is big for Plunker but the ONLY files of concern is: index.html , module.js , controller.js , unitTest.js . index.html contains <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.3.3/jasmine.min.css"> <script src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.3.3/jasmine.min.js"></script> <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.3.3/jasmine-html.min.js"></script> <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.3.3/boot.min.js"></script> <script type="text/javascript" src="https://code.angularjs.org/1.4.0-rc.2/angular-mocks.js"></script> module.js contains: myTable = angular.module('myTable', ['ngMdIcons', 'ngAnimate', 'ngSanitize', 'ui.bootstrap']); controller.js contains the controller myTable.controller('tableCtrl', ['$scope', '$sce', '$timeout', '$filter', 'orderByFilter', '$uibModal', 'factGlobal', 'factApp', 'factMainTbl', '$window', '$element', '$log', '$http', 'APIService', '$document', function ($scope, $sce, $timeout, $filter, orderBy, $uibModal, factGlobal, factApp, factMainTbl, $window, $element, $log, $http, APIService, $document) { where factGlobal, factApp, etc. are services in the perspective *.js files At the end of the controller is the function I am trying to test. Found it on the web. // =============================================================================================================== // Eample unit test function // =============================================================================================================== $scope.sum = function () { $scope.z = $scope.x + $scope.y; }; }]) // end controller unitTest.js <<<<< CONTAINS UNIT TEST THAT IS CRASHING with the error calculatorExample encountered a declaration exception describe('calculatorExample', function () { beforeEach( // module.js contains // // myTable = angular.module('myTable', ['ngMdIcons', 'ngAnimate', 'ngSanitize', 'ui.bootstrap']); module('myTable', ['ngMdIcons', 'ngAnimate', 'ngSanitize', 'ui.bootstrap'] // Crashes // module('myTable', [] // myTable = angular.module('myTable', ['ngMdIcons', 'ngAnimate', 'ngSanitize', 'ui.bootstrap']); ) ); // Controller: // myTable.controller('tableCtrl', ['$scope', '$sce', '$timeout', '$filter', 'orderByFilter', '$uibModal', 'factGlobal', 'factApp', 'factMainTbl', '$window', '$element', '$log', '$http', 'APIService', '$document', // function ($scope, $sce, $timeout, $filter, orderBy, $uibModal, factGlobal, factApp, factMainTbl, $window, $element, $log, $http, APIService, $document) { var $controller; beforeEach(inject(function (_$controller_) { $controller = _$controller_; })); describe('sumExample', function () { it('1 + 1 should equal 2', function () { var $scope = {}; var controller = $controller('tableCtrl', { $scope: $scope }); // All these have crashed // $scope, $sce, $timeout, $filter, 'orderBy', $uibModal, 'factGlobal', 'factApp', 'factMainTbl', $window, $element, $log, $http, 'APIService', $document // $scope, $sce, $timeout, $filter, orderBy, $uibModal, factGlobal, factApp, factMainTbl, $window, $element, $log, $http, APIService, $document // $scope: $scope, $sce:$sce, $timeout:$timeout, $filter:$filter, orderBy:orderBy, $uibModal:$uibModal, factGlobal:factGlobal, factApp:factApp, factMainTbl:factMainTbl, $window:$window, $element:$element, $log:$log, $http:$http, APIService:APIService, $document:$document $scope.x = 1; $scope.y = 2; $scope.sum(); expect($scope.z).toBe(3); }); }); }); When I get it running I will correct it on Plunker and make it public. Perhaps it will also be useful to someone else. -- You received this message because you are subscribed to the Google Groups "Angular and AngularJS discussion" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at https://groups.google.com/group/angular. For more options, visit https://groups.google.com/d/optout.
