Reputation: 43
I am currently experimenting with AngularJS. I have created a simple service, which is initialized from some distant data. The url is passed as an 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, I am trying to unit test this, but I cannot manage to correctly set the "someUrl" parameter. I have tried things like 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, I always receive a "No module: someUrl" error. What is the correct syntax to initialize an app value during a unit test?
Upvotes: 4
Views: 4999
Reputation: 23394
You setted value to "myApp" module and is testing "myApp.services" module. You should change module('myApp.services')
to module('myApp')
.
Anyway, there is a better way to do so. You can use mock module
to inject $provide
so you can override any dependency.
This is how your spec would look like:
describe('Services', function() {
// here we load the module myApp and load an additional one that
// allows you to override your 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});
}));
});
});
Upvotes: 6