Reputation: 624
So I am learning how to use karma. And I have read several tutorials on how to inject scope into my project, but I am still getting errors on
Error: [$injector:unpr] Unknown provider: $scopeProvider <- $scope http://errors.angularjs.org/1.6.3/$injector/unpr?p0=%24scopeProvider%20%3C-%20%24scope
Here is my spec:
describe('InfoController', function() {
var InfoController, state;
beforeEach(angular.mock.module('ptoApp.controller'));
var $controller
var scope;
beforeEach(inject(function($controller,$scope, $state,$rootScope, employeeTestFactory) {
scope = $rootScope.$new();
InfoController = $controller('InfoController', {
$scope: scope,
state: $state,
employeeTestFactory: employeeTestFactory
});
}));
// Verify our controller exists
it('should be defined', function() {
expect(InfoController).toBeDefined();
});
});
And here is my controller:
.controller('InfoController', ['$scope', '$state','$rootScope', 'employeeTestFactory', function ($scope, $state, $rootScope, employeeTestFactory) {
$scope.loggedIn = {};
$scope.displaySection = true;
$rootScope.callInfo=function(){
employeeTestFactory.get($rootScope.email).then(function(result) {
$scope.loggedIn = result;
});
}
$scope.getPercentUsed = function(){
if(typeof $rootScope.employee === 'undefined' || typeof $rootScope.employee.totalTimeUsed === 'undefined' || typeof $rootScope.employee.totalTimeAccrued === 'undefined') return 0;
var percentage = ($rootScope.employee.totalTimeUsed/$rootScope.employee.totalTimeAccrued)*100;
return (percentage > 100) ? 100 : percentage;
}
$scope.hideSession = function(){
$scope.displaySection = !$scope.displaySection;
}
}])
Now I have heard for scope, it is useful to attach a new rootScope to it right away, so I added scope = $rootScope.$new(); and then set it to $scope: scope, when creating the controller. Still getting the error. Thanks in advance for any help.
Upvotes: 0
Views: 90
Reputation: 533
In your karma spec file, remove $scope
from your inject
function params:
- beforeEach(inject(function($controller,$scope, $state,$rootScope, employeeTestFactory) {
+ beforeEach(inject(function($controller, $state, $rootScope, employeeTestFactory) {
Upvotes: 1