I'm writing an application with AngularJs 1.4.4 and have just embarked on TDD for the first time. I'm using Karma with Jasmine and have had no trouble testing expressions on the $scope, however when trying to test and expression defined on 'this' in a Controller it returns as undefined. Angular indicates that using 'this' in your controller is best practise but I've not found a clear example of testing.
Here is my Controller
'user_strict';
var app = angular.module('app', ['ngRoute', 'ngAnimate']);
angular.module('app')
app.controller('LoginCtrl', ['$scope', function($scope) {
var login = this;
login.user = {message:'hello'};
$scope.userName = "Anthony";
}])
My test script
'use strict';
describe('Controller: LoginCtrl', function() {
// load the controller's module
beforeEach(module('app'));
var LoginCtrl,
scope;
// initalize the controller and a mock scope
beforeEach(inject(function ($controller, $rootScope) {
scope = $rootScope.$new();
LoginCtrl = $controller('LoginCtrl', {
$scope: scope,
});
}));
it('should equal to equal to Anthony', function() {
expect(scope.userName).toBe("Anthony");
});
it('login user should equal to hello', function() {
expect(login.user.message).toBe('hello');
})
});
The first test passes but the second return this error/fail;
Controller: LoginCtrl login user should equal to hello FAILED
TypeError: 'undefined' is not an object (evaluating 'login.user.message')
My presumption is that it needs to be injected like the controller and the scope but the methods I've tried haven't worked. Any help is much appreciated :)
Aucun commentaire:
Enregistrer un commentaire