This is my first attempt to use Karma / Jasmine. I want to use it to test an AngularJS application.
This is a simple Hello World test to make sure everything works, but it doesn’t.
I am trying to test a controller called InboxController. I defined a scope variable: $ scope.greeting = 'Hello World'.
I am trying to verify this in a separate file called controller-test.js.
This is a test:
'use strict';
describe ("Controller tests", function () {
describe('InboxController', function () {
var scope;
beforeEach(angular.mock.module('myApp'));
beforeEach(angular.mock.inject(function($rootScope, $controller){
scope = $rootScope.$new();
$controller('InboxController', {$scope: scope});
}));
it('should return Hello World', function () {
expect(scope.greeting).toBe('Hello World');
});
});
});
I get an error message:
It is not possible to read the undefined property greeting, which is part of scope.greeting in the test code.
Karma configuration:
files : [
'bower_components/angular/angular.js',
'bower_components/angular-mocks/angular-mocks.js',
'bower_components/angular-resource/angular-resource.js',
'app.js',
'js/*.js',
'test/*.js'
],
InboxController:
app.controller('InboxController', function($rootScope, $scope,
$location, InboxService) {
$scope.greeting = 'Hello World';
$rootScope.loggedIn = true;
$scope.loadInbox = function() {
$scope.emails = InboxService.getMessages().success(function(jsonData) {
$scope.emails = jsonData;
});
}
$scope.delete = function (id) {
InboxService.delete(id).success(function() {
$scope.loadInbox();
});
};
$scope.loadInbox();
});