AngularJS controller test failing with RequireJS -


my jasmine test (using karma) throws error : argument 'saleslistcontroller' not function, got undefined

after searching lot question seems close error, unit test not e2e scenario test.

my test-main.js

(function (window, require) { 'use strict'; var file, requiremodules; requiremodules = [];  (file in window.__karma__.files) {     if (window.__karma__.files.hasownproperty(file)) {        // console.log('loaded file'+ file);         if (file.substring(file.length - 26, file.length) === 'saleslistcontrollertest.js') {             console.log('added file testing..');             requiremodules.push(file);         }     } }  //requiremodules.push('appmodule'); //requiremodules.push('mocks'); deps: requiremodules, require({     baseurl: '',     paths:{         'angular': '/base/app/bower_components/angular/angular',         'angularresource': '/base/app/bower_components/angular-resource/angular-resource',         'angularmocks': '/base/app/bower_components/angular-mocks/angular-mocks',         'appmodule': '/base/app/scripts/appmodule',         'approutes':'/base/app/scripts/approutes',         'services/dependencyresolverfor':'/base/app/scripts/services/dependencyresolverfor',         'saleslistcontroller' : '/base/app/scripts/controllers/saleslistcontroller'     },     shim:{         'angular' :{             exports:'angular'         },         'approutes': {exports:'approutes'},         'services/dependencyresolverfor' : {exports:'services/dependencyresolverfor'},         'appmodule': {             deps: ['approutes', 'services/dependencyresolverfor'],             exports: 'appmodule'         },         'angularresource': {             deps: ['angular'],             exports: 'angularresource'         },         'angularmocks': {             deps: ['angularresource'],             exports: 'angularmocks'         } ,         'saleslistcontroller': {             deps: ['appmodule'],             exports: 'saleslistcontroller'         }     } }, requiremodules, function () {     window.__karma__.start(); }, function (err) {     var failedmodules = err.requiremodules;     console.log("err", err);      if (failedmodules && failedmodules[0]) {         throw new error("module not loaded: " + failedmodules);     } else {         throw new error("unknown error:" + err);     } });   }(window, require)); 

my example.spec

define(['appmodule','angular', 'angularresource', 'angularmocks','saleslistcontroller'], function(app, angular, angularresource, angularmocks, saleslstctrl) {     describe('saleslistcontroller1', function(){         beforeeach(module('angularsamplebhoomiapp'));         var saleslistcontroller, scope;         var sales = [{customer:"a1",number:1,id:1},{customer:"b1",number:2,id:2}];         beforeeach(inject(function($controller, $rootscope,$injector){         scope = $rootscope.$new();         saleslistcontroller = $controller('saleslistcontroller', {$scope:scope, getallsalesresolved:sales});     }));     it('should have 0 items when loaded', function(){         expect(scope.sales).tobeundefined();     }); });}); 

my example controller

define(['appmodule'], function(myapp){ function saleslstctrl(){     myapp.lazy.controller('saleslistcontroller' ,['$scope', 'getallsalesresolved',         function ($scope, sales) {             console.log('before sales');             $scope.sales= sales;             console.log( $scope.sales.length);         }]);                          } return saleslstctrl;}); 

since using lazy loading resolving dependencies :

define([], function() { return function(dependencies) {     var definition =     {         resolver: ['$q','$rootscope', function($q, $rootscope)         {             var deferred = $q.defer();              require(dependencies, function()             {                 $rootscope.$apply(function()                 {                     deferred.resolve();                 });             });              return deferred.promise;         }]     }     return definition; } }); 

my github repo entire sample here

pl help.

yes, have found solution.

here blog post same.

github code here : sample

it done through mocking module creation.

edit : code. trick here initialize angularjs :

define(function(){ var app = angular.module('angularappmodule', 'ngresource']);app.lazy = app;  return app;}); 

this add app.lazy reference in angular, , requirejs can initialize controllers same way. test-main.js file same. running karma, test-main load scripts upfront , require not intrusive. let me know if have further questions.


Comments

Popular posts from this blog

scala - 'wrong top statement declaration' when using slick in IntelliJ -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

PySide and Qt Properties: Connecting signals from Python to QML -