describe('Testing a controller', function() { // chargement du module controllers beforeEach(module("myApp")); var ctrl, scope, httpMock; // injection du service $controller, du $rootScope, du $httpBackend beforeEach(inject(function($controller, $rootScope, $httpBackend) { httpMock = $httpBackend; // création d'un nouveau scope scope = $rootScope.$new(); // simulation d'un requete GET sur la resource data/patients.json httpMock.when('GET', 'data/patients.json').respond([{name: 'name1'}, {'name': 'name2'}]); //httpMock.expectGET('data/patients.json').respond([{name: 'name1'}, {'name': 'name2'}]); // création du controller avec le nouveau scope, le mock de $http ctrl = $controller("listController", { $scope: scope }); httpMock.flush(); })); // test de récupération des données patients it('test load patients', function() { // la chaine de caractère "name1" est elle bien dans le premier objet du tableau ? expect(scope.patients[0].name).toMatch("name1"); }); // test de la méthode addPatient() it('test add patient', function() { scope.patient = "test"; scope.addPatient(); // la chaine de caractère "test" est elle bien dans le dernier objet du tableau ? expect(scope.patients[scope.patients.length-1].name).toMatch("test"); }); afterEach(function() { httpMock.verifyNoOutstandingExpectation(); httpMock.verifyNoOutstandingRequest(); }); }); angular.module('myApp',[]). controller("listController", ["$scope", "$http", "$log", function ($scope, $http, $log) { $http.get("data/patients.json"). success( function (data, status, headers, config) { $scope.patients = data; }). error(function (data, status, headers, config) { $log.error("error getting movies : " + data); }); $scope.patient; $scope.addPatient = function () { $scope.patients.push({name:$scope.patient}); $scope.patient = null; }; $scope.removePatient = function (i) { $scope.patients.splice(i, 1); } }]); (function() { var jasmineEnv = jasmine.getEnv(); jasmineEnv.updateInterval = 1000; var trivialReporter = new jasmine.TrivialReporter(); jasmineEnv.addReporter(trivialReporter); jasmineEnv.specFilter = function(spec) { return trivialReporter.specFilter(spec); }; $(function() { jasmineEnv.execute(); }); })();