I am facing an issue while writing a unit test for a controller in my application. Jasmine is showing an 'Unknown provider' error related to a provider I created for fetching template URLs. This provider is injected into a config function that is used in routes.js.
The specific error message reads:
Error: Unknown provider: assetPathProvider
Below is the configuration for Karma:
files: [
'vendor/assets/javascripts/jquery.js',
'vendor/assets/javascripts/angular.js',
'spec/javascripts/lib/angular/angular-mocks.js',
'vendor/assets/javascripts/angular-*.js',
'vendor/assets/javascripts/*.js',
'app/assets/javascripts/initialize.js',
'app/assets/javascripts/**/*.js',
'spec/javascripts/unit/**/*.js'
],
I've initialized my app as follows:
Viewfinder = angular.module('viewfinder', [
'ui.bootstrap',
'scroll',
'ngCookies',
'ngResource',
'chart',
'http-auth-interceptor',
'facebook-connect',
'twitter-connect',
'Alerts',
'smartTable.table',
'ngClipboard',
'angularFileUpload'
])
Here's the beginning of routes.js
Viewfinder.config(['$routeProvider', '$locationProvider', 'assetPathProvider', function($routeProvider, $locationProvider, assetPathProvider) {
The assetPathProvider is essential for retrieving the correct template location in routes.js
...
templateUrl: assetPathProvider.get('welcome/signed_in.html'),
....
Now, let's take a look at the provider itself:
Viewfinder.provider('assetPath', [function() {
this.get = function(path) {
if(angular.isDefined(gon.config.manifest)) {
return '/assets/' + gon.config.manifest[path]
} else {
return '/assets/' + path
}
}
this.$get = function() {
return {
get: this.get
}
}
}]);
Although I have simplified my spec, I'm still encountering the Unknown provider error. Here is the spec:
describe('OneSheetPackagesViewController', function() {
var $rootScope, $scope, $controller, message
beforeEach(function() {
module('viewfinder', function(assetPathProvider) {})
})
beforeEach(inject(function(_$rootScope_) {
message = 'hello'
}))
it("should successfully submit a comment", function() {
console.log(message)
expect(message).toBeDefined()
})
})