i new testing, have angular experience. developed angular application want add tests grip on jasmine , karma. setup karma, added simple 'getgreeting' function 1 of services in angular app , added test file (/test/utilsservice.spec.js) jasmine test. fails because service undefined (angular-mocks.js added). code:
karma.conf.js:
// base path used resolve patterns (eg. files, exclude) basepath: '', // frameworks use // available frameworks: https://npmjs.org/browse/keyword/karma-adapter frameworks: ['jasmine'], // list of files / patterns load in browser files: [ 'node_modules/**/*.js', 'app/**/*.js', 'test/utilsservice.spec.js' ], // list of files exclude exclude: [ ], // 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_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, // concurrency level // how many browser should started simultaneous concurrency: infinity
test file in /test/utilsservice.spec.js:
describe('getgreeting',function(){ var utilsservice; beforeeach(angular.mock.module('app.ontdekjouwtalent')); beforeeach(inject( function(_utilsservice_) { utilsservice = _utilsservice_; } )); it('should test dummy function', function(){ expect(1+1).toequal(2); expect(utilsservice.getgreeting("marc")).toequal("hello marc"); } ) });
notice comment out loading / testing service load app: app.ontdekjouwtalent. utilsservice in /app/shared/services/utilsservice.js
angular.module('app.ontdekjouwtalent'). service('utilsservice',['appconfig',function(appconfig){ this.debug = function(data){ if(appconfig.appconstants_islocal){ return data; } } this.getgreeting = function(name){ return "hello " + name; } }])
the angular app defined elsewhere - in /app/app.js this:
angular.module('app.ontdekjouwtalent', [ 'angular-storage', 'ui.bootstrap', 'ui.router', 'ui.router.modal', 'xeditable', 'angular-confirm', 'ui.select', 'ngsanitize', 'angular-growl', 'nganimate' ])
when running webroot directory (wwwroot) in cmd window "karma start"
i:\www\ontdekjouwtalent\wwwroot>karma start 04 08 2016 19:23:32.633:warn [karma]: no captured browser, open http://localhost:9876/ 04 08 2016 19:23:32.756:info [karma]: karma v1.1.2 server started @ http://localhost:9876/ 04 08 2016 19:23:32.757:info [launcher]: launching browser chrome unlimited concurrency 04 08 2016 19:23:32.769:info [launcher]: starting browser chrome 04 08 2016 19:23:38.634:info [chrome 51.0.2704 (windows 10 0.0.0)]: connected on socket /#cxn5ven8tqblj23oaaaa id 50497607 chrome 51.0.2704 (windows 10 0.0.0) error uncaught referenceerror: module not defined @ node_modules/abbrev/abbrev.js:2
so 'module' undefined... have no clue how tackle this. wrong here?
ok, turns out had add lot more files in karma.conf.js files section! specifically:
// list of files / patterns load in browser files: [ 'node_modules/angular/angular.js', 'node_modules/angular-ui-bootstrap/dist/ui-bootstrap-tpls.js', 'node_modules/ui-select/dist/select.js', 'node_modules/angular-mocks/angular-mocks.js', 'node_modules/angular-growl-v2/build/angular-growl.min.js', 'node_modules/angular-ui-router/release/angular-ui-router.min.js', 'node_modules/angular-storage/dist/angular-storage.min.js', 'node_modules/angular-animate/angular-animate.min.js', 'node_modules/angular-sanitize/angular-sanitize.min.js', 'node_modules/angular-xeditable/dist/js/xeditable.min.js', 'node_modules/angular-ui-router/release/angular-ui-router.min.js', 'node_modules/angular-ui-router-uib-modal/angular-ui-router-uib-modal.js', 'node_modules/angular-confirm/angular-confirm.js', 'node_modules/angular-aria/angular-aria.min.js', 'node_modules/angular-messages/angular-messages.min.js', 'app/app.js', 'app/shared/services/apiservice.js', 'app/shared/services/sessionservice.js', 'app/login/authservice.js', 'app/translation/translationservice.js', 'app/shared/services/utilsservice.js', 'test/utilsservice.spec.js' ],
not quite clear why have include these services... utilsservice isn't needing them. app includes 7 services, not 5. also, why there no need include controllers (app uses +9)?
Comments
Post a Comment