angularjs - Karma + requirejs: Karma not able to run my angular modules and controllers -


karma + angularjs + requirejs: karma not able run angular modules , controllers. when trying execute tests gives me error: " error: [ng:areq] argument 'democontroller' not function, got undefined"

i pretty new angular , karma/requirejs test frameworks. found couple of posts online set karma+require angular project. followed steps mentioned here http://karma-runner.github.io/0.8/plus/requirejs.html http://monicalent.com/blog/2015/02/11/karma-tests-angular-js-require-j/

i referred couple of posts on stack overflow said may missing module in controller defined. after doing getting same error. think there missing in terms of either configuration or paths js files in karma.conf and/or test-main.js.

here how project structure looks like

  • angularforkarma
    • lib
      • angular.js
      • angular.min.js
      • require.js
      • angular-mocks.js
  • node_modules
  • src
    • main.js
    • app.js
    • controller
      • democontroller.js
  • test
    • karma.conf
    • test-main.js
    • controller
      • test.js
  • package.json

here how each file looks

  • main.js - main file require.js

    requirejs.config({ baseurl : "/src", paths : {  angular : '../lib/angular.min',  // main controller  main_ctrl : '/src/democontroller', },  shim : {  angular : {     exports : "angular"  }, } });  require([ 'angular', 'main_ctrl' ], function() { 'use strict';  console.log('inside main.js');  angular.bootstrap(document, [ 'angularforkarma' ]); }); 
  • app.js

    define([ 'angular' ], function(angular) {   var app = angular.module('angularforkarma', []);   return app; }); 
  • democontroller.js

    define([ 'angular', 'app'], function(angular, angularforkarma) { angularforkarma.controller("democontroller", function($scope) {     $scope.comparevalues = function(value1, value2) {         console.log('comparevalues.................');         $scope.text = 'hello world!';         if (angular.equals(value1, value2)) {             return true;         }          return false;     };     $scope.comparevalues(); }); }); 
    • karma.conf

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

    // base path used resolve patterns (eg. files, exclude) basepath: '../..',   // frameworks use // available frameworks: https://npmjs.org/browse/keyword/karma-adapter frameworks: ['jasmine', 'requirejs'],   // list of files / patterns load in browser files: [   'angularforkarma/test/test-main.js',   {pattern: 'angularforkarma/lib/*.js', included: false},   {pattern: 'angularforkarma/src/**/*.js', included: false},   {pattern: 'angularforkarma/test/**/*.js', included: false} ],   // list of files exclude exclude: [     'angularforkarma/src/main.js' ],   // preprocess matching files before serving them browser // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor preprocessors: { },   // test results reporter 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 output (reporters , 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_debug,   // enable / disable watching file , executing tests whenever 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 tests , exits singlerun: false,  // concurrency level // how many browser should started simultanous concurrency: infinity })} 
    • test-main.js

    /

    //get list of test files include var alltestfiles = []; var test_regexp = /(spec|test)\.js$/i; object.keys(window.__karma__.files).foreach(function(file) {   if (test_regexp.test(file)) {     // normalize paths requirejs module names.     // if require sub-dependencies of test files loaded as-is (requiring file extension)     // not normalize paths     // var normalizedtestmodule = file.replace(/^\/base\/|\.js$/g, '');     // alltestfiles.push(normalizedtestmodule);     } });  (var file in window.__karma__.files) {   if (test_regexp.test(file)) {     alltestfiles.push(file);   } }  require.config({ // karma serves files under /base, basepath config file baseurl: '/base/angularforkarma/src', // dynamically load test files deps: alltestfiles,  // have kickoff jasmine, asynchronous callback: window.__karma__.start,  paths: {  angular: '../lib/angular',  angularmocks: '../lib/angular-mocks',  // main controller  main_ctrl : '../src/controller/democontroller' }, shim: {  angular: { exports: 'angular' },  angularmocks: { deps: ['angular'] } } }); 

here how test file looks

  • test.js

    use strict'; define(['angular', 'app', 'angularmocks'], function (angular, app) { describe('democtrl', function() { var scope; // we'll use these in our tests  // mock application allow inject our own dependencies beforeeach(angular.mock.module('angularforkarma'));  // mock controller same reason , include $rootscope , $controller beforeeach(angular.mock.inject(function($rootscope, $controller) {     console.log("inside beforeeach");     // create empty scope     scope = $rootscope.$new();      // declare controller , inject our empty scope     $controller('democontroller', {         $scope : scope     }); }));  // tests start here it('should have variable text = "hello world!"', function() {     console.log("inside first test");     expect(scope.text).tobe('hello world!'); }); it('should compare values"', function() {     console.log("inside 2nd test");     expect(scope.comparevalues('anup', 'anup')).tobe(true); }); // it('should compare values"', function() { //  expect(scope.comparevalues('anup', 'shreyas')).tobe(true); // }); }); }); 

can tell me making mistake? stuck on last 3-4 days still not able figure out why not loading controllers , modules

thanks


Comments

Popular posts from this blog

java - Static nested class instance -

c# - Bluetooth LE CanUpdate Characteristic property -

JavaScript - Replace variable from string in all occurrences -