lundi 16 février 2015

AngularJS Mocking

I've got no idea what's happening here. I'm trying to mock out a service and inject it inside a controller.


My first test passes but then my second test gives the following error:



Error: [ng:areq] http://ift.tt/1DjwZVp
at Db (/Users/PrimeByDesign/Documents/Development/ADashOfPanache/framework/src/libs/bower/angular/angular.min.js:19)
at Ya (/Users/PrimeByDesign/Documents/Development/ADashOfPanache/framework/src/libs/bower/angular/angular.min.js:19)
at /Users/PrimeByDesign/Documents/Development/ADashOfPanache/framework/src/libs/bower/angular/angular.min.js:67
at /Users/PrimeByDesign/Documents/Development/ADashOfPanache/framework/src/js/functionality/authentication/authenticationSpec.js:51
at d (/Users/PrimeByDesign/Documents/Development/ADashOfPanache/framework/src/libs/bower/angular/angular.min.js:35)
at workFn (/Users/PrimeByDesign/Documents/Development/ADashOfPanache/framework/src/libs/bower/angular-mocks/angular-mocks.js:2177)
Expected h({ $$childTail: null, $$childHead: null, $$nextSibling: null, $$watchers: null, $$listeners: Object({ }), $$listenerCount: Object({ }), $id: '004', $$childScopeClass: null, this: <circular reference: Object>, $parent: h({ $id: '003', $$childTail: <circular reference: Object>, $$childHead: <circular reference: Object>, $$prevSibling: null, $$nextSibling: null, $$watchers: null, $parent: null, $$phase: null, $root: <circular reference: Object>, this: <circular reference: Object>, $$destroyed: false, $$asyncQueue: [ ], $$postDigestQueue: [ ], $$listeners: Object({ }), $$listenerCount: Object({ }), $$isolateBindings: Object({ }), $$childScopeClass: Function }), $$prevSibling: null }) to be null.
at /Users/PrimeByDesign/Documents/Development/ADashOfPanache/framework/src/js/functionality/authentication/authenticationSpec.js:79


The code is as follows (authenticationSpec.js):



var mockAuthenticationService = "";
var $controllerConstructor = "";
var scope = {};
var ctrl = {};
describe("Authentication Controller", function () {


//you need to indicate your module in a test
beforeEach(module('app'));

// before each test inject the following
beforeEach(inject(function($controller, $rootScope){

// create new controller and root scope.
$controllerConstructor = $controller;
scope = $rootScope.$new();

// Create the Mock authentication service.
angular.module('app', function($provide) {
$provide.service('authenticationService', mockAuthenticationService);
});


mockAuthenticationService = {};

//setup the fake service data for a successful login.
mockAuthenticationService.data = {
id: 1,
user: { id:1, role:'admin'}
};

// create a fake login implementation on our mock object
mockAuthenticationService.login = function (credentials) {

// setup result as null.
var result = null;

// simulate remote server authenticating.
if (credentials.username == 'kiran' && credentials.password == 'kiranPassword') {
result = this.data;
}

return result;
};

ctrl = $controllerConstructor('authenticationController', { $scope: scope, authenticationService: mockAuthenticationService});

}));



it("will retrieve user data for the correct credentials", function(){

var credentials = {
username: 'kiran',
password: 'kiranPassword'
};
var resultData = scope.login(credentials);

expect(resultData.id).toEqual(1);
expect(resultData.user.role).toEqual('admin');

});


it('will return null for a user with incorrect credentials', function(){

var credentials = {
username: 'BADGUY!',
password: 'HAXOR'
};

var resultData = scope;
expect(resultData).toBeNull();
});
});


I've also got the test files declaration:



var testFiles = [
'framework/src/libs/bower/angular/angular.min.js',
'framework/src/libs/bower/angular-route/angular-route.min.js',
'framework/src/libs/bower/angular-mocks/angular-mocks.js',
'framework/src/js/app.js',
'framework/src/js/functionality/authentication/*Service.js',
'framework/src/js/functionality/authentication/*Controller.js',
'framework/src/js/functionality/authentication/*Spec.js'
];


As far as I can see the above test files array looks correct.


I'm really new to Angular and I've just been trying to work through it so I'd appreciate any advice you can give me.


Aucun commentaire:

Enregistrer un commentaire