单元测试AngularJS控制器失败

时间:2016-11-04 13:38:42

标签: angularjs unit-testing karma-jasmine

尝试测试控制器时出错。我查了

  

'/ [...] /温度/ 31f42b3cc460dde1f27c1199258bced7.browserify:1'

但文件为空;

错误:

04 11 2016 14:24:36.699:INFO [framework.browserify]: bundle updated 04 11 2016 14:24:36.700:INFO [watcher]: Changed file "C:/Users/../Documents/GitHub/timetracker2/tests/public/init.controllers.js". 04 11 2016 14:24:36.709:INFO [watcher]: Changed file "C:/Users/../AppData/Local/Temp/31f42b3cc460dde1f27c1199258bced7.browserify". Chrome 54.0.2840 (Windows 10 0.0.0) ERROR   Uncaught Error: bundle error (see logs)   at C:/Users/../AppData/Local/Temp/31f42b3cc460dde1f27c1199258bced7.browserify:1

sidenav.controller.js - 我想测试的控制器。

(function(){
  'use strict';

  angular.module('app.layout', [])
  .controller('SideNavController', SideNavController);

  function SideNavController(){
    var vm = this;
    vm.name = "SideNavController"
  }


})();

tests \ public \ init.controllers.js - 我的测试代码。

var assert = require('assert');

describe("sideNavCtrl", function() {
  var controller,
      scope;

  beforeEach(angular.mock.module('app.layout'));

  beforeEach(angular.mock.inject(function($controller, $rootScope){
        scope = $rootScope.$new();
        controller = $controller('SideNavController', {
          $scope: scope;
        });
  }));

  it('should have a SideNavCtrl controller', function() {
    expect(controller).toBeDefined();
  })


})

app.module.js - 根模块

 angular.module('app', [
      /* AngularJS modules */
      'ngMaterial',
      'ngSanitize',
      'ngAnimate',
        /* app.feature modules */
      'app.layout'
      /* cross.app modules */

    ]);

karma.config.js

// Karma configuration
// Generated on Thu Nov 03 2016 11:57:48 GMT+0100 (Środkowoeuropejski czas stand.)

module.exports = function(config) {
  config.set({

    // base path that will be used to resolve all patterns (eg. files, exclude)
    basePath: '',


    // frameworks to use
    // available frameworks: https://npmjs.org/browse/keyword/karma-adapter
    frameworks: ['jasmine', 'browserify'],


    // list of files / patterns to load in the browser
    // 1. add angularj.js directory
    // 2. add angular-mock
    // 3. add test files
    files: [
      /* angular files */
      './../node_modules/angular/angular.js',
      './../node_modules/angular-mocks/angular-mocks.js',
      './../node_modules/angular-aria/angular-aria.js',
      './../node_modules/angular-animate/angular-animate.js',
      './../node_modules/angular-messages/angular-messages.js',
      './../node_modules/angular-material/angular-material.js',
      './../node_modules/angular-material/angular-material-mocks.js',
      './../node_modules/angular-sanitize/angular-sanitize.js',
      /* my scripts */
      './../public/layout/sidenav.controller.js',

      './../public/app.module.js',
      './../public/app.config.js',
      './../public/app.run.js',
      /* test files */
      './../tests/**/*.js'
      /* backend files */
    ],


    // list of files to exclude
    exclude: [
    ],


    // preprocess matching files before serving them to the browser
    // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
    preprocessors: {
      './../tests/**/*.js': [ 'browserify' ]
    },


    // test results reporter to use
    // possible values: 'dots', 'progress'
    // available reporters: https://npmjs.org/browse/keyword/karma-reporter
    reporters: ['progress'],


    // web server port
    port: 9876,


    // enable / disable colors in the output (reporters and logs)
    colors: true,


    // level of logging
    // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
    logLevel: config.LOG_INFO,


    // enable / disable watching file and executing tests whenever any file changes
    autoWatch: true,


    // start these browsers
    // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
    browsers: ['Chrome'],


    // Continuous Integration mode
    // if true, Karma captures browsers, runs the tests and exits
    singleRun: false,

    // Concurrency level
    // how many browser should be started simultaneous
    concurrency: Infinity
  })
}

我的项目树: project tree

2 个答案:

答案 0 :(得分:1)

beforeEach(angular.mock.inject(function($controller, $rootScope){
   scope = $rootScope.$new();
   controller = $controller('SideNavController', function(){
      $scope: scope;
    });

你忘了在$ controller

中写函数()

答案 1 :(得分:0)

问题是' ; '在init.controllers.js

应该是:

 $scope: scope