Bonjour à tous,

Depuis plusieurs jours, je me tire les cheveux car je ne comprends rien à mes tests.
ControllerSubscribe.js est mon fichier où se fait la récupération des données que l'utilisateur a renseigné depuis un formulaire HTML, puis l'envoie de ces données à l'API.
Je souhaiterais faire des tests unitaires sur la fonctionnalité de mon inscription de l'API mais je n'arrive à rien !

Ci-dessous, le code des fichiers.
Dans le code du test unitaire (testSubscribe.js), j'ai écris en commentaire ce qu'il devrait se passer mais ce qui se passe à la fin.

fichier : ControllerSubscribe.js
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
var app = angular.module("Subscribe", []);
app.controller("ControllerSubscribe", function($scope, $http, $window){
 
  $scope.Subscribe = function (){
    var hash = {};
      hash['Janvier'] = 01;
      hash['Février'] = 02;
      hash['Mars'] = 03;
      hash['Avril'] = 04;
      hash['Mai'] = 05;
      hash['Juin'] = 06;
      hash['Juillet'] = 07;
      hash['Août'] = 08;
      hash['Septembre'] = 09;
      hash['Octobre'] = 10;
      hash['Novembre'] = 11;
      hash['Décembre'] = 12;
 
    var day = $scope.Day;
    if ($scope.Day <= 9) {
      day = "0"+ $scope.Day;
    }
    var month = $scope.Month;
    if (hash[month])
      {
        month = hash[month];
        if (month <= 9)
          month = "0" + month;
      }
    var year = $scope.Year;
    var birthdate = day + month + year;
 
    var tab = [];
    if ($scope.informatique)
      tab.push("informatique");
    if ($scope.menage)
      tab.push("menage");
    if ($scope.bricolage)
      tab.push("bricolage");
    if ($scope.enseignement)
      tab.push("enseignement");
    if ($scope.gardiennage)
      tab.push("gardiennage");
    if ($scope.jardinnage)
      tab.push("jardinnage");
    var compétences = angular.toJson(tab, false);
 
    $http.post('http://umannity.com:1337/subscribe',
    { birthdate:birthdate,
      email:$scope.Email,
      first_name:$scope.FirstName,
      last_name:$scope.LastName,
      password:$scope.Pwd,
      skills:compétences,      
      sexe:$scope.Sexe
      }).success(function (data) {
        //$window.location.href = 'acceuil.html';
      }) .error(function (data, status) {
        $scope.message= "  " + data.error.message;
      })
  }
});
fichier : testSubscribe.js
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//Initialisation du Test de ControllerUser.js
describe("Test de ControllerSubscribe.js", function() {
 
// Récupération du module souhaité (USER dans ControllerUser.js)
 beforeEach(module('Subscribe'));
  var $scope, controller;
 
//Récupération du Controller et du Scope (Scope contient les variables)
  beforeEach(inject(function ($rootScope, $controller) {
    $scope = $rootScope.$new();
    controller = $controller('ControllerSubscribe', {
    $scope: $scope
    });
  }));
 
  describe('Testing the subscribe route', function() {
 
    //Test de la route http://umannity.com:1337/subscribe sans envoyer les données d'inscription, devrait donc renvoyer une erreur " error: "Missing parameter password", code: 422 "
    it('should call /subscribe with no data', inject(function($httpBackend) {
      $scope.Subscribe();
      $httpBackend.expectPOST('http://umannity.com:1337/subscribe')
       .respond([{message: "OK"}]);
      $httpBackend.flush();
    }));
    /* D'après KARMA, il s'agit d'un SUCCESS.
       Peu importe ce que je mets dans .respond() j'ai un success.
    */
 
 
    //Test de la route http://umannity.com:1337/subscribe en envoyant les données d'inscription, devrait donc renvoyer 
    // en Response Body  { message: "OK" }
    // en Response Code 201
    it('should SUCCESS to create an account', inject(function($httpBackend) {
      $scope.Subscribe();
      $httpBackend.expectPOST('http://umannity.com:1337/subscribe',
                          {
                            birthdate:"03021993",
                            email:"test@test.fr",
                            first_name:"John",
                            last_name:"Doe",
                            password:"test",
                            skills: [
                              "couture",
                              "grand ecart"
                            ]
                          }
                        )
       .respond([{message: "OK"}]);
      $httpBackend.flush();
    }));
    /* Error: Expected POST http://umannity.com:1337/subscribe with different data
        EXPECTED: {"birthdate":"03021993","email":"test@test.fr","first_name":"John","last_name":"Doe","password":"test","skills":["couture","grand ecart"]}
        GOT:      {"birthdate":null,"skills":"[]"}
    */
 
  });
});