angularjs - How to inject External Providers in karma jasmine test case? -


i'm new angular , karma test cases.

for application i'm using angularjs , sweetalert displaying alerts. ( sweetalert plugin here )

below code getting status http request , display http request status in sweetalert

(function () { 'use strict';  angular     .module('app.admin')     .controller('testcontroller', testcontroller);  testcontroller.$inject = ['$scope','$http','sweetalert']; function testcontroller($scope,$http,sweetalert) { var vm = this;  vm.test = 'hi';     vm.todos = [];     vm.todo = 'run';     vm.status = '';  vm.geturl = function(){     $http.get("some url")                     .then(function (result) {                         vm.status = result.status;                         sweetalert.swal(json.stringify(vm.status))                     }); };  vm.addtodo = function(){     vm.todos.push(vm.todo); }; vm.removetodo = function(index){     vm.todos.splice(index, 1); };  }  })(); 

this working fine without problems, need test application, using karma jasmine framework. below karma.config.js , test file.

karma.config.js

// karma configuration // generated on tue jan 26 2016 21:38:16 gmt+0530 (india standard time)  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'],       // list of files / patterns load in browser     files: [     '../app/js/base.js',     '../global_url.js',     'bower_components/sweetalert/dist/sweetalert.css',     'bower_components/sweetalert/dist/sweetalert.min.js',     'bower_components/angular-mocks/angular-mocks.js',     '../app/js/app.js',     'test/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: false,       // start these browsers     // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher     browsers: ['chrome'],     plugins: [                 'karma-chrome-launcher',                 'karma-firefox-launcher',                 'karma-jasmine',                 'karma-phantomjs-launcher'             ],       // continuous integration mode     // if true, karma captures browsers, runs tests , exits     singlerun: false,      // concurrency level     // how many browser should started simultaneous     concurrency: infinity   }) } 

test.spec.js

'use strict';  describe('test controller', function () {    // load controller's module   var mainctrl,         sweetalert,         urlservice,         httpcall,         scope;   beforeeach(module('sample'));    /*beforeeach(inject(function(_sweetalert_,_urlservice_){     urlservice = _urlservice_;     sweetalert = _sweetalert_;   }));*/    // initialize controller , mock scope   beforeeach(inject(function ($rootscope, $controller,$http,_sweetalert_) {     scope = $rootscope.$new();     mainctrl = $controller('testcontroller', {       $scope: scope,       httpcall : $http,       sweetalert : _sweetalert_     });   }));      it('should tell hi', function () {       expect(mainctrl.test).tobe("hi");     });     it('should give status', function () {         mainctrl.geturl();       expect(mainctrl.status).tobe(200);     });    it('should have no items start', function () {     expect(mainctrl.todos.length).tobe(0);   });    it('should add items list', function () {     mainctrl.todo = 'test 1';     mainctrl.addtodo();     expect(mainctrl.todos.length).tobe(1);   });    it('should add remove item list', function () {     mainctrl.todo = 'test 1';     mainctrl.addtodo();     mainctrl.removetodo(0);     expect(mainctrl.todos.length).tobe(0);   });  }); 

but throws following error:

    f:\projects\angular\newsample\development\test\master>gulp test     [13:06:40] using gulpfile f:\projects\angular\newsample\development\test\master\gulpfile.js     [13:06:40] starting 'test'...     warn `start` method deprecated since 0.13. removed in 0.14. please      use       server = new server(config, [done])       server.start()     instead.     27 01 2016 13:06:40.250:info [karma]: karma v0.13.19 server started @ http://lo     calhost:9876/     27 01 2016 13:06:40.261:info [launcher]: starting browser chrome     27 01 2016 13:06:41.793:info [chrome 47.0.2526 (windows 8.1 0.0.0)]: connected o     n socket /#hmyjfo5x2tltkmmhaaaa id 83740230     chrome 47.0.2526 (windows 8.1 0.0.0) log: 'localhost'      chrome 47.0.2526 (windows 8.1 0.0.0) log: 'localhost'      chrome 47.0.2526 (windows 8.1 0.0.0) test controller should tell hi failed             error: [$injector:unpr] unknown provider: sweetalertprovider <- sweetale     rt             http://errors.angularjs.org/1.4.2/$injector/unpr?p0=sweetalertprovider%2     0%3c-%20sweetalert                 @ f:/projects/angular/newsample/development/test/ap     p/js/base.js:9274:12 

i don't know why., how can solve this?

for take long week, possible include third party plugins in karma.

please me, in advance.

before inject should add line.

beforeeach(module('_sweetalert_')); 

and change karma.config.js file path below.

bower_components/sweetalert/dist/sweetalert.min.js app/bower_components/sweetalert/dist/sweetalert.min.js


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -