AngularJS单元测试崩溃。茉莉花

AngularJS单元测试崩溃。茉莉花,angularjs,jasmine,Angularjs,Jasmine,我正在尝试用AngularJS做我的第一个单元测试,但一直都不顺利。我尝试运行在web上找到的单元测试示例失败。几个小时后,我需要一些帮助 我已经把密码写在普朗克身上了。有人能帮忙吗? 这对Plunker来说是个大问题,但唯一值得关注的文件是: index.html、module.js、controller.js、unitTest.js html包含 <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare

我正在尝试用AngularJS做我的第一个单元测试,但一直都不顺利。我尝试运行在web上找到的单元测试示例失败。几个小时后,我需要一些帮助

我已经把密码写在普朗克身上了。有人能帮忙吗?

这对Plunker来说是个大问题,但唯一值得关注的文件是: index.html、module.js、controller.js、unitTest.js

html包含

<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>  
controller.js包含控制器

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) {  
在哪里 factGlobal、factApp等是透视图*.js文件中的服务

在控制器的末尾是我要测试的功能。 在网上找到的

    // ===============================================================================================================
    // Eample unit test function
    // ===============================================================================================================
    $scope.sum = function () {
        $scope.z = $scope.x + $scope.y;
    };
}]) // end controller

unitTest.js基本上在库的顺序上存在一个问题。还使用了angular-mocks.js的最新版本

看看我更新的plunker


单元测试崩溃是因为各种控制器对象(“$scope”、“$sce”、“$timeout”等)未正确映射到单元测试中带有局部变量的单元测试

在Plunker上进行了更正:


我将angular-mocks.js版本更新为最新版本。你所说的“图书馆秩序”是什么意思?在*.js库或unitTest.js中?检查my index.html。您需要按特定顺序引用javascript库。另外,请检查Unittest.js文件和控制器文件中的更改。快速尝试了*.js文件,但将整个过程搞砸了。我现在理解了您所说的Index.js中*.js文件的顺序,我将在明天重试。谢谢。我按你说的订购了Index*.js文件。在引导之前放置jQuery。从您那里复制了unitTest.js。撞车。我尝试了其他方法。撞车。你能给我看带有我的控制器的unitTest.js而不是破坏你的controller.js吗?谢谢@Kalyan
    // ===============================================================================================================
    // Eample unit test function
    // ===============================================================================================================
    $scope.sum = function () {
        $scope.z = $scope.x + $scope.y;
    };
}]) // end controller
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);  
        });  
    });  
});  
myTable.controller('tableCtrl', ['$scope',
function ($scope) {


    // ===============================================================================================================
    // Eample unit test function
    // ===============================================================================================================
    $scope.sum = function () {
        $scope.z = $scope.x + $scope.y;
    };

}]) // end controller
// controller.js
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) {  


// unitTest.js
describe('Hello World example ', function() {
    beforeEach(module('myTable'));
    var tableCtrl;
    var scope;
    var sce;
    var timeout;
    var filter;
    var orderByLocal;
    var uibModalLocal;
    var factGlobalLocal;
    var factAppLocal;
    var factMainTblLocal;
    var window;
    var element;
    var log;
    var http;
    var APIServiceLocal;
    var document;

    beforeEach(inject(function ($rootScope, $controller) {
        scope = $rootScope.$new();
        tableCtrl = $controller('tableCtrl', {
            $scope: scope, $sce: sce, $timeout: timeout, $filter: filter, orderBy: orderByLocal, $uibModal: uibModalLocal, factGlobal: factGlobalLocal, factApp: factAppLocal, factMainTbl: factMainTblLocal, $window: window, $element: element, $log: log, $http: http, APIService: APIServiceLocal, $document: document
        });
    }));
});