As I attempt to create a test for my home module, I keep encountering an error that reads "unknown provider: service." Interestingly, when I modify resolveSomething in my home module to output a string, the app functions as expected, indicating that the resolve is functioning properly. Since I am still in the process of learning how to write tests, any guidance or recommendations would be greatly valued.
Service
angular.module( 'services', [])
.factory('service', function(){
return {
'users' : function(id) {
return id;
}
}
});
Relevant part of Home
angular.module('home', ['ui.router','services'])
.config(function config( $stateProvider, service) {
$stateProvider.state( 'homeWithId', {
url: '/home/:id',
views: {
"main": {
controller: 'HomeCtrl',
templateUrl: 'home/home.tpl.html'
}
},
resolve:{
resolveSomething: function() {
return service.users(this.params.id);
}
}
})
})...
Relevant part of Test... I think
describe( 'Test home controller', function() {
beforeEach( module( 'home' ) );
beforeEach( module( 'services' ) );
beforeEach(inject(function($injector) {
$location = $injector.get('$location');
$rootScope = $injector.get('$rootScope');
$httpBackend = $injector.get('$httpBackend');
$scope = $rootScope.$new();
$state = $injector.get('$state');
service = $injector.get('service');
var $controller = $injector.get('$controller');
createController = function() {
return $controller('HomeCtrl', {
'$scope': $scope,
resolveSomething: function() { console.log('resolveSomething'); },
service : service
});
};
...
By the way, it's worth noting that with this test setup, I can access the service within my controller if I simply provide a string in the resolve.
Home controller (functional and testable)
.controller( 'HomeCtrl', ['$scope', 'resolveSomething','service', function( $scope, resolveSomething, service) {
alert(service.users(1));
...