javascript - Unit test on Angularjs with Jasmine and Webpack, error Unknown provider -
i ask how solve error won't let me write test. angularjs factory (service.es6) test:
import rx 'rxjs/rx' var themiddlemodule = angular.module('themiddlemodule', []) themiddlemodule.factory('themiddleop', function ($http){ var api = 'someurl' var themiddleop = {} themiddleop.getssoidfromservice = () => { return rx.observable.frompromise( $http({ method: 'get', url: api }) ) } return themiddleop })
this test webpack configuration:
var jasminewebpackplugin = require('jasmine-webpack-plugin'); module.exports = { entry: ['./app/scripts/test/service.js'], plugins: [new jasminewebpackplugin()], output: { path: __dirname, filename: 'test.js' }, module: { loaders: [ { test: /\.es6$/, exclude: /(node_modules|bower_components)/, loader: 'babel?presets[]=es2015!eslint-loader' }, { test: /\.js$/, exclude: /(node_modules|bower_components)/, loader: 'babel?presets[]=es2015!eslint-loader' } ] } } };
and js test file:
import 'angular'; import 'angular-mocks'; import '../services/service.es6'; var service; describe('service test', function() { var $httpbackend, $httpprovider; var module = angular.mock.module(); var url = 'someurl'; beforeeach(function () { module('themiddlemodule'); inject(function ( _$httpbackend_, _themiddleop_) { $httpbackend = _$httpbackend_; service = _themiddleop_; $httpbackend.when('post', url) .respond(200, {'sso-id':'test'}); }); }); it('$httpbackend should defined', function (){ expect($httpbackend).tobedefined(); }); it('themiddleop should defined', function (){ expect(service).tobedefined(); }); it('should call ssoid service', function (){ var getfromservice = service.getssoidfromservice() getfromservice.subscribe( (response) => {}, (e) => {}, () => { $httpbackend.expectget(url); $httpbackend.flush(); } ) }); aftereach(function() { $httpbackend.verifynooutstandingexpectation(); $httpbackend.verifynooutstandingrequest(); }); });
the error jasmine gives me is:
error: [$injector:unpr] unknown provider: themiddleopprovider <- themiddleop
if comment row:
service = _themiddleop_;
the $httpbackend test passes correctly , fails others, of course. tried import service.es6 without export, module default export , more combinations. in advance give me.
it looks module not defined. because factory registered module. error in module definition.
there 3 things wrong if may guess:
var module = angular.mock.module();
why needed? why not write angular.mock.module in before each?
angular.mock.module('themiddlemodule');
the other option factory , choice of variables. suggestion rewrite followed:
angular.module('themiddlemodule', []) .factory('themiddleop', function ($http){ var api = 'someurl'; return { getssoidfromservice = () => { return rx.observable.frompromise( $http({ method: 'get', url: api }); ) }; });
the last option webpack configuration wrong. can debug test configure not singlerun;
last comment, unit test not testing anything; , remove check if $httpbackend there because testing angular mock working?
Comments
Post a Comment