miuosh miuosh - 1 month ago 27
AngularJS Question

Unit test AngularJS controller failed

I get an error while trying test controller. I check the


'/[...]/Temp/31f42b3cc460dde1f27c1199258bced7.browserify:1'


but file is empty;

error:

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 - controller I want to test.

(function(){
'use strict';

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

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


})();


tests\public\init.controllers.js - My test code.

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 - root module

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
})
}


My project tree:
project tree

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

You forgot write function() in $controller

Comments