Reputation: 1875
I am new with karma-jasmine and trying to develop a demo test case. I am getting error of scope is not defined in 'it'. I have read the following links with same problem but it's not helping me with my test case.
Error Karma 'undefined' scope variable in beforeEach
TypeError: $scope is undefined in Angular controller unit test with Jasmine
This is my karma.conf.js
module.exports = function(config) {
config.set({
exclude)
basePath: '',
frameworks: ['jasmine'],
files: [
'js/angular.js',
'js/angular-mocks.js',
'app.js',
'test/**/*Spec.js'
],
exclude: [
],
preprocessors: {
},
reporters: ['progress'],
port: 9876,
colors: true,
config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
logLevel: config.LOG_INFO,
autoWatch: false,
browsers: ['Firefox'],
singleRun: false
});
};
Here is my mySpec.js where test code is written
describe('myApp', function() {
beforeEach(module('myApp'));
describe('HelloWorldController', function() {
var scope,HelloWorldController;
beforeEach(inject(function ($rootScope, $controller) {
scope = $rootScope.$new();
HelloWorldController = $controller('HelloWorldController', {
$scope: scope
});
}));
it('should assign message to hello world', function () {
expect(scope.greeting).toEqual('Hello World!');
});
});
});
And this is my app.js where controller is defined.
var myApp = angular.module('myApp',[]);
myApp.controller('HelloWorldController', ['$scope', function($scope) {
$scope.greeting = 'Hello World!';
}]);
The error which I get is,
TypeError: scope is undefined in** /home/abc/WebstormProjects/test1/test/basic/mySpec.js (line 17)
I don't have any idea of where I am making mistake. Please guide me toward this issue.
Thanks in advance.
Upvotes: 1
Views: 2174
Reputation: 127
One of your problems here was a typo. 'scope' was undefined, because in your controller injection, you defined it as '$scope', but your expect statement referenced 'scope':
describe('myApp', function() {
beforeEach(module('myApp'));
describe('HelloWorldController', function() {
var scope,HelloWorldController;
beforeEach(inject(function ($rootScope, $controller) {
scope = $rootScope.$new();
HelloWorldController = $controller('HelloWorldController', {
$scope: scope
});
}));
it('should assign message to hello world', function () {
expect(scope.greeting).toEqual('Hello World!');
});
});
});
Upvotes: 1