Wednesday 15 January 2014

angularjs with requirejs and karma testing - inject undefined -



angularjs with requirejs and karma testing - inject undefined -

i have project using angularjs requirejs , testing karma. having trying testing service in project.

these files have configured angularjs, requirejs, , karma. when run service test error typeerror: cannot read property 'inject' of undefined. if comment out beforeeach part in test code runs fine, can't in real tests.

test-main.js

class="lang-js prettyprint-override">var alltestfiles = [], file; (file in window.__karma__.files) { if (window.__karma__.files.hasownproperty(file)) { if(/test\.js$/.test(file)) { alltestfiles.push(file); } } } require.config({ baseurl: '/base', // alias library paths // must set 'angular' paths: { 'jquery': 'web/common/js/vendor/jquery-1.10.2/jquery-1.10.2.min', /* mutual includes */ 'angular' : 'web/common/js/vendor/angular/angular', 'angular-ui-router' : 'web/common/js/vendor/angular/angular-ui-router.min', 'angular-css-injector' : 'web/common/js/vendor/angular/angular-css-injector', 'angular-animate' : 'web/common/js/vendor/angular/angular-animate.min', 'angularamd' : 'web/common/js/vendor/angular/angularamd', 'ssss-abs-tpls' : 'web/common/js/sandbox/ssss-abs-tpls', 'angular-sanitize' : 'web/common/js/sandbox/angular-sanitize', 'modernizr' : 'web/common/js/vendor/modernizr/modernizr', 'scrolltoplugin' : 'web/common/js/vendor/scrolltoplugin/scrolltoplugin', 'tweenmax' : 'web/common/js/vendor/tweenmax/tweenmax', 'gestures' : 'web/common/js/vendor/gestures/gestures', 'ssss-base' : 'web/common/js/framework/ssss.base', 'hammer' : 'web/common/js/framework/hammer', 'angular-mocks' : 'web/common/js/vendor/angular/angular-mocks', /* controllers,not listed*/ /* services */ 'myservice' : 'app/services/models/myservice', /* directives, not listed*/ /* router */ 'myapp' : 'app/myapp', /*utils*/ 'message' : 'app/utils/message' }, /* angular modules not back upwards amd out of box, specify them in shim */ shim: { 'angular-ui-router' : ['angular'], 'gestures': ['angular'], 'angular-sanitize': ['angular'], 'angular-css-injector': ['angular'], 'angular-animate': ['angular'], 'ssss-session': ['angular'], 'ssss-abs-tpls' : ['angular','gestures','angular-sanitize'], 'angular-mocks' : ['angular'] }, /* kick start application */ //deps: ['emaintenance'], deps: alltestfiles, // have kickoff jasmine, asynchronous callback: window.__karma__.start });

karma.conf.js

class="lang-js prettyprint-override">// karma configuration // generated on wed nov 05 2014 15:22:19 gmt-0600 (central standard time) module.exports = function(config) { config.set({ // base of operations path used resolve patterns (eg. files, exclude) basepath: './', // frameworks utilize // available frameworks: https://npmjs.org/browse/keyword/karma-adapter frameworks: ['jasmine', 'requirejs'], // list of files / patterns load in browser files: [ {pattern: 'web/common/*.js', included: false}, {pattern: 'web/common/**/*.js', included: false}, {pattern: 'app/*.js', included: false}, {pattern: 'app/**/*.js', included: false}, {pattern: 'test/*.js', included:false}, {pattern: 'test/**/*.js', included:false}, {pattern: 'test-main.js', included: true} ], // list of files exclude exclude: [ /*requirejs paths file*/ 'app/main.js' ], // preprocess matching files before serving them browser // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor preprocessors: { }, // test results reporter utilize // 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_info, // 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, plugins:[ 'karma-requirejs', 'karma-chrome-launcher', 'karma-jasmine', 'karma-phantomjs-launcher' ] }); };

myservice.js

class="lang-js prettyprint-override">define(['myapp'], function (app) { app.register.factory("myservice",["$http","$q", function($http, $q) { getdata:function(){ var info = { "test1":"some data" }; homecoming { then:function(myfunc){ myfunc(data); } }; } }; }]); });

myservice.test.js

class="lang-js prettyprint-override">define(['angular-mocks','myapp','myservice'], function (mocks, app, myservice) { describe('myservice test', function(){ var service; beforeeach(mocks.inject(function(myservice){ service = myservice; })); it('should something', function(){ expect('abc').tobe('abc'); }); }); });

just phone call inject not mocks.inject ! should works !

beforeeach(inject(function(myservice){ service = myservice; }));

angularjs testing requirejs jasmine karma-runner

No comments:

Post a Comment