AngularJS Karma&茉莉花单元测试总是失败

时间:2016-11-14 06:39:44

标签: javascript unit-testing firebase jasmine karma-runner

我是Karma& amp;的初学者。茉莉花单元测试。

这是我写的单元测试代码

describe('test site.js', function() {
describe('test profileCtrl.js',function(){
    var $scope = {};
    beforeEach(module('tfApp'));
    beforeEach(inject(function($rootScope,$controller){
        $scope = $rootScope.$new();
        $controller('profileCtrl',{$scope: $scope});
    }));
    it('check function edit()',function(){
        $scope.profile_readOnly = true;
        $scope.edit();
        expect($scope.profile_readOnly).toEqual(false);
    });
});
});

这是我的javascript代码:

app.controller("profileCtrl",  
function($scope, Auth, $firebaseArray, $firebaseObject,$window, $stateParams) { 
    Auth.$onAuthStateChanged(function(authData){
        //initialize
        if (authData) {
            $scope.authData = authData;
            ref = firebase.database().ref("users/"+$scope.authData.uid+"/readOnly/info");
            $scope.profile_info = $firebaseObject(ref);
            $scope.profile_readOnly = true;
            $scope.profile_readOnly_checkbox = "return false;"
            //$scope.profile_info.tag.c++=false;
            var id = $stateParams.uid;
            if (id != $scope.authData.uid)  $scope.button_visible = false;
            else $scope.button_visible = true;
            console.log(authData);
        }
        else {
            console.log("signed out");
        }
    });
    $scope.button_name = "EDIT";
    $scope.edit = function(){           
        if($scope.profile_readOnly) {
            $scope.profile_readOnly=false;
            $scope.button_name = "SAVE";
        }
        else{
            $scope.profile_info.$save().then(function(){
                console.log($scope.profile_info);
            });
            $scope.profile_readOnly=true;
            $scope.button_name = "EDIT";
        }
    };
}
);

这是我的karam.conf.js文件:

module.exports = function(config) {

config.set({

basePath: './app',
frameworks: ['jasmine'],
files: [
  'lib/jquery.min.js',
         'lib/angular.min.js',
      'lib/angular-route.min.js',
      'lib/angular-mocks.js',
  'lib/firebase.js',
  'lib/angularfire.min.js',   
  'js/main.js',
  'js/site.js',
  'js/main.js',
  'js/homeCtrl.js',
  'js/notificationCtrl.js',
  'js/dashboard.js',
  'js/requestCtrl.js',
  'js/profileCtrl.js',
  'js/eventCtrl.js',
  'js/login.js',
  'js/teamCtrl.js',
  'js/eventDCtrl.js',
  'js/helper.js',
       'unit_tests/*.js'      
],
exclude: [
],
preprocessors: {        
     'js/site.js' : ['coverage'],       
     'js/index.js' : ['coverage'],
     'js/admin.js' : ['coverage'],
     'js/team.js' : ['coverage'],
     'js/member.js' : ['coverage']
},
reporters: ['progress', 'coverage'],
coverageReporter: {
        type: 'html',
        dir: 'coverage/',
        subdir: '.'
},
port: 8080,
colors: true,
browsers: ['Chrome'],
singleRun: true,
plugins: [
  'karma-chrome-launcher',      
  'karma-jasmine',
  'karma-coverage'
]    

当我运行npm测试时,它给出的答案是:

Chrome 53.0.2785 (Linux 0.0.0) test site.js test profileCtrl.js check function edit() FAILED
Error: [$injector:modulerr] http://errors.angularjs.org/1.5.7/$injector/modulerr?p0=tfApp&p1=Error%3A%20%5B%24injector%3Amodulerr%5D%20http%3A%2F%2Ferrors.angularjs.org%2F1.5.7%2F%24injector%2Fmodulerr%3Fp0%3Dui.router%26p1%3DError%253A%2520%255B%2524injector%253Anomod%255D%2520http%253A%252F%252Ferrors.angularjs.org%252F1.5.7%252F%2524injector%252Fnomod%253Fp0%253Dui.router%250A%2520%2520%2520%2520at%2520Error%2520(native)%250A%2520%2520%2520%2520at%2520http%253A%252F%252Flocalhost%253A8080%252Fbase%252Flib%252Fangular.min.js%253F022f3ec8815b7e846d0701328f8128543729a616%253A6%253A412%250A%2520%2520%2520%2520at%2520http%253A%252F%252Flocalhost%253A8080%252Fbase%252Flib%252Fangular.min.js%253F022f3ec8815b7e846d0701328f8128543729a616%253A25%253A72%250A%2520%2520%2520%2520at%2520b%2520(http%253A%252F%252Flocalhost%253A8080%252Fbase%252Flib%252Fangular.min.js%253F022f3ec8815b7e846d0701328f8128543729a616%253A24%253A115)%250A%2520%2520%2520%2520at%2520http%253A%252F%252Flocalhost%253A8080%252Fbase%252Flib%252Fangular.min.js%253F022f3ec8815b7e846d0701328f8128543729a616%253A24%253A358%250A%2520%2520%2520%2520at%2520http%253A%252F%252Flocalhost%253A8080%252Fbase%252Flib%252Fangular.min.js%253F022f3ec8815b7e846d0701328f8128543729a616%253A39%253A374%250A%2520%2520%2520%2520at%2520r%2520(http%253A%252F%252Flocalhost%253A8080%252Fbase%252Flib%252Fangular.min.js%253F022f3ec8815b7e846d0701328f8128543729a616%253A7%253A355)%250A%2520%2520%2520%2520at%2520g%2520(http%253A%252F%252Flocalhost%253A8080%252Fbase%252Flib%252Fangular.min.js%253F022f3ec8815b7e846d0701328f8128543729a616%253A39%253A222)%250A%2520%2520%2520%2520at%2520http%253A%252F%252Flocalhost%253A8080%252Fbase%252Flib%252Fangular.min.js%253F022f3ec8815b7e846d0701328f8128543729a616%253A39%253A391%250A%2520%2520%2520%2520at%2520r%2520(http%253A%252F%252Flocalhost%253A8080%252Fbase%252Flib%252Fangular.min.js%253F022f3ec8815b7e846d0701328f8128543729a616%253A7%253A355)%0A%20%20%20%20at%20Error%20(native)%0A%20%20%20%20at%20http%3A%2F%2Flocalhost%3A8080%2Fbase%2Flib%2Fangular.min.js%3F022f3ec8815b7e846d0701328f8128543729a616%3A6%3A412%0A%20%20%20%20at%20http%3A%2F%2Flocalhost%3A8080%2Fbase%2Flib%2Fangular.min.js%3F022f3ec8815b7e846d0701328f8128543729a616%3A40%3A134%0A%20%20%20%20at%20r%20(http%3A%2F%2Flocalhost%3A8080%2Fbase%2Flib%2Fangular.min.js%3F022f3ec8815b7e846d0701328f8128543729a616%3A7%3A355)%0A%20%20%20%20at%20g%20(http%3A%2F%2Flocalhost%3A8080%2Fbase%2Flib%2Fangular.min.js%3F022f3ec8815b7e846d0701328f8128543729a616%3A39%3A222)%0A%20%20%20%20at%20http%3A%2F%2Flocalhost%3A8080%2Fbase%2Flib%2Fangular.min.js%3F022f3ec8815b7e846d0701328f8128543729a616%3A39%3A391%0A%20%20%20%20at%20r%20(http%3A%2F%2Flocalhost%3A8080%2Fbase%2Flib%2Fangular.min.js%3F022f3ec8815b7e846d0701328f8128543729a616%3A7%3A355)%0A%20%20%20%20at%20g%20(http%3A%2F%2Flocalhost%3A8080%2Fbase%2Flib%2Fangular.min.js%3F022f3ec8815b7e846d0701328f8128543729a616%3A39%3A222)%0A%20%20%20%20at%20Object.db%20%5Bas%20injector%5D%20(http%3A%2F%2Flocalhost%3A8080%2Fbase%2Flib%2Fangular.min.js%3F022f3ec8815b7e846d0701328f8128543729a616%3A43%3A246)%0A%20%20%20%20at%20Object.workFn%20(http%3A%2F%2Flocalhost%3A8080%2Fbase%2Flib%2Fangular-mocks.js%3F0b7de425bd8504ad3dbae2be21385f4a577c0109%3A3067%3A52)
    at Error (native)
    at lib/angular.min.js:6:412
    at lib/angular.min.js:40:134
    at r (lib/angular.min.js:7:355)
    at g (lib/angular.min.js:39:222)
    at Object.db [as injector] (lib/angular.min.js:43:246)
    at Object.workFn (lib/angular-mocks.js:3067:52)
TypeError: $scope.edit is not a function
    at Object.<anonymous> (unit_tests/test_site.js:11:11)

我已经尝试了几天这个单元测试。请帮助我,非常感谢。

0 个答案:

没有答案