AngularJS - Unit testing service with value dependency injection -
i experimenting angularjs. have created simple service, initialized distant data. url passed application value.
//app declaration angular.module('myapp', ['myapp.services']).value('someurl', 'http://www.example.com'); //service declaration angular.module('myapp.services', ['someurl']).factory('myservice', function(someurl) { var data; "do stuff" return data; });
now, trying unit test this, cannot manage correctly set "someurl" parameter. have tried things this, without succes:
angular.module('myapp', ['myapp.services']).value('someurl', 'test/test.json'); describe('services', function() { beforeeach(module('myapp.services')); describe('myservice', function() { it('should return {success: true}', inject(function() { expect(data).toequal({success: true}); })); }); });
however, receive "no module: someurl" error. correct syntax initialize app value during unit test?
you setted value "myapp" module , testing "myapp.services" module. should change module('myapp.services')
module('myapp')
.
anyway, there better way so. can use mock module
inject $provide
can override dependency.
this how spec like:
describe('services', function() { // here load module myapp , load additional 1 // allows override dependency beforeeach(module('myapp', function($provide) { $provide.value('someurl', 'test/test.json'); })); describe('myservice', function() { it('should return {success: true}', inject(function() { expect(data).toequal({success: true}); })); }); });
Comments
Post a Comment