不加载karma-jasmine框架中生成的测试用例

时间:2018-05-28 14:53:14

标签: angularjs karma-jasmine angular-controller karma-coverage angular-unit-test

我正在尝试为控制器编写一个简单的测试用例,但它不会给出任何错误,也不会加载我编写的测试用例。

app.js

var myApp = angular.module('myApp',[ 'ui.bootstrap', 'dialogs', 'ui.router','base64','ngtimeago',
        'cfp.loadingBar', 'ngStorage', 'ngCookies', 'ngTable','ngRoute',
        'angularUtils.directives.dirPagination', 'ngSanitize','ngTagsInput',
        'ngIdle','angular.filter','angular-encryption','ngImgCrop']);

loginController.js

myApp.controller("loginController",['$scope','$rootScope','$state','APIServices','$http','$localStorage','cfpLoadingBar','$log','sha256','MFEEDBACK_CONSTANT_MESSAGES',
        function($scope, $rootScope, $state, APIServices, $http,$localStorage,cfpLoadingBar,$log,sha256) {
    var lc = this;
    lc.test = "hello";
}

loginController.specs.js

describe('Controllers', function(){
    beforeEach(module('myApp'));
    describe('loginController', function(){
        var loginController;
        beforeEach(inject(function($controller,$rootScope){
            var state,mockStorage,APIService,mockHTTP,mockLoadingBar,log,sha256,messages;
            var mockScope,mockRootScope = $rootScope.$new();
            loginController = $controller('loginController',{
                $scope:mockScope,$rootScope:mockRootScope,$state:state,APIServices:APIService,$http:mockHTTP,
                $localStorage:mockStorage,cfpLoadingBar:mockLoadingBar,$log:log,sha256:sha256,MFEEDBACK_CONSTANT_MESSAGES:messages
            });
        }));
        it('testCase1val should be working', function(){
            expect(loginController.test).toBe('hello');
        });
    });
});

获得输出“执行0 of 0 ERROR(0.005秒/ 0秒)”

getting output "Executed 0 of 0 ERROR (0.005 secs / 0 secs)"

1 个答案:

答案 0 :(得分:0)

我没有在app.js文件中添加第三方依赖项。 最重要的是我们需要在'karma.conf.js'中添加所有必需的第三方javascript文件。 然后在单元测试用例中添加这些依赖项,我们需要创建模拟javascript文件并命名为“ui.router.js”。
并确保该文件应包含以下代码。

'use-strict';
angular.module('ui.router', []);

我们需要为'app.js'文件中的每个依赖声明创建模拟javascript文件,并在'karma.conf.js'中添加这些文件。
然后在加载app模块之前加载它们,如下所示。< br />

beforeEach(function(){
   module('ui.router');
   module('ngTagsInput');
   .
   .
   .
   module('all your third party depedencies.');
});
beforeEach(module('myApp'));