2016-08-22 1 views
2

Ich habe einen einfachen Controller in meiner eckigen App, und die Jasmin-Testspezifikation für denselben gibt einen Referenzfehler zurück. Mein Controller-Code:Referenzfehler in Jasmine-Tests: Variable kann nicht gefunden werden

'use strict'; 
angular.module('taskListAppApp') 
    .controller('MainCtrl', function ($scope) { 
    $scope.todoList = [{ 
     todoText: 'In case of Fire', 
     done: false 
    }, { 
     todoText: 'git commit', 
     done: false 
    }, { 
     todoText: 'git push', 
     done: false 
    }, { 
     todoText: 'exit the building!', 
     done: false 
    }]; 


    $scope.getTotalTodos = function() { 
     return $scope.todoList.length; 
    }; 


    $scope.todoAdd = function() { 
     // Checking for null or empty string 
     if (null !== $scope.taskDesc && "" !== $scope.taskDesc) { 
      $scope.todoList.push({ 
       todoText: $scope.taskDesc, 
       done: false 
      }); 
     } 
    }; 

    // Function to remove the list items 
    $scope.remove = function() { 
     var oldList = $scope.todoList; 
     $scope.todoList = []; 
     angular.forEach(oldList, function (x) { 
      if (!x.done) { 
       $scope.todoList.push(x); 
      } 
     }); 
    }; 
}); 

Und meine Testspezifikation:

"use strict" 

    describe('Controller: MainCtrl', function() {  //describe your object type 
     // beforeEach(module('taskListNgApp2App')); //load module 
     beforeEach(angular.mock.module('taskListAppApp')); 
     describe('MainCtrl', function() { //describe your app name 

      var todoCtrl2; 
      beforeEach(inject(function ($controller, $rootScope) { 
       var scope = $rootScope.$new(); 
       todoCtrl2 = $controller('MainCtrl', { 
        //What does this line do? 
        $scope: scope 
       }); 
      })); 

      it('should have todoCtrl defined', function() { 
       expect(todoCtrl2).toBeDefined(); 
      }); 

     it('trial test for toEqual', function(){ 
      var a = 4; 
      expect(a).toEqual(4); 
     }); 
//THESE 2 FAIL 
     it('should have todoList defined', function() { 
      expect(scope.todoList).toBeDefined(); 
     }); 

     it('should have add method defined', function(){ 
      expect(todoCtrl2.todoAdd()).toBeDefined(); 
     }); 

    }); 
}); 

Der Fehler, den ich bekommen ist:

PhantomJS 2.1.1 (Linux 0.0.0) Controller: MainCtrl MainCtrl should have add method defined FAILED 
    TypeError: undefined is not a function (evaluating 'todoCtrl2.todoAdd()') in test/spec/controllers/main.spec.js (line 58) 
    test/spec/controllers/main.spec.js:58:28 
    [email protected]://localhost:8080/context.js:151:17 
PhantomJS 2.1.1 (Linux 0.0.0): Executed 4 of 4 (2 FAILED) (0.05 secs/0.02 secs) 

ich andere Wege versucht, die Objekte/Funktionen aufrufen, aber die die letzten 2 Tests versagen jedes Mal mit demselben Fehler, nämlich ReferenceError

Wo werde ich die Objekte aufrufen?

+0

Dies liegt daran, dass Sie den Wert nach der Methode todoAdd zurück testen definiert werden, aber ich hoffe, dass die Absicht, diese Methode auf Umfang definiert zu überprüfen ist. Also versuchen Sie es unten: es ('sollte Add-Methode definiert haben', Funktion() { expect (todoCtrl2.todoAdd). ToBeDefined(); }); – Harpreet

+0

@Harpreet - Ich habe versucht, Ihre Lösung wird zusammen mit dem man Jay schlug unten, aber ich habe immer noch diese Fehlermeldung: PhantomJS 2.1.1 (Linux 0.0.0) Controller: MainCtrl MainCtrl sollte nicht definiert haben add-Methode FAILED definiert \t Erwartete zu definieren. –

Antwort

0

Sie müssen var scope außerhalb der Funktion deklarieren. Ihre Bereichsvariable ist in Ihrem Testfall nicht definiert.

Versuchen Sie, diese

describe('Controller: MainCtrl', function() {  //describe your object type 
    var scope;  
    // beforeEach(module('taskListNgApp2App')); //load module 

    beforeEach(angular.mock.module('taskListAppApp')); 
    describe('MainCtrl', function() { //describe your app name 

     var todoCtrl2; 
     beforeEach(inject(function ($controller, $rootScope) { 
      scope = $rootScope.$new(); 
      todoCtrl2 = $controller('MainCtrl', { 
       //What does this line do? 
       $scope: scope 
      }); 
     })); 

     it('should have todoCtrl defined', function() { 
      expect(todoCtrl2).toBeDefined(); 
     }); 

     it('trial test for toEqual', function(){ 
      var a = 4; 
      expect(a).toEqual(4); 
     }); 
    //THESE 2 FAIL 
     it('should have todoList defined', function() { 
      expect(scope.todoList).toBeDefined(); 
     }); 

     it('should have add method defined', function(){ 
      expect(scope.todoAdd).toBeDefined(); 
     }); 

    }); 
}); 
+0

Danke Jay! Ihre Lösung hat funktioniert ... irgendwie. Aber eine Frage, dieser Teil es ('sollte TodoList definiert haben', Funktion() { erwarten (scope.todoList). ToBeDefined(); }); Ergebnisse in PhantomJS 2.1.1 (Linux 0.0.0) Controller: MainCtrl MainCtrl sollte Add-Methode definiert haben FAILED \t Erwartet undefined definiert werden. Wie kann ich also auf die todoList zugreifen und prüfen, ob sie definiert ist? –

+0

Sie können korrekte ANS und Upvote akzeptieren, wenn es hilft! –

+0

Hat das .. :) Können Sie mit der anderen Sache aushelfen? –

Verwandte Themen