2014-02-26 25 views
5

我想編寫一個單元測試,看看我的控制器中的'getStudents()'提供者函數是否被調用,如果一些屬性被適當設置。注意.success()回調:Karma測試.success()得到'undefined'不是一個對象'

$scope.update = function update() { 
    // omitted, just doing some checking... 
    // finally 
    else if (key.length === 3 || $scope.students.length === 0) { 
     StudentsProvider.getStudents($scope.keyword, $scope.selectedFilters).success(function(data) { 
      $scope.students = data; 
     }); 
    } 
}; 

我的人緣單元測試是這樣的:

describe("Students: Controllers", function() { 
    var $scope; 
    var ctrl; 
    beforeEach(module('studentsApp')); 

    describe("SearchCtrl", function() { 
     // Mock the provider 
     var mockStudentsProvider = { 
      getStudents: function getStudents() { 
       return [ 
        { 
         Education: [], 
         Person: [{ 
          ID: 1, 
          Name: "Testing McTestsson", 
          SSN: "1234567890", 
          Address: "Fakestreet 3", MobilePhone: "7777777" 
         }] 
        } 
       ]; 
      } 
     }; 
     var StudentsProvider; 
     beforeEach(inject(function ($controller, $rootScope) { 
      $scope = $rootScope.$new(); 
      ctrl = $controller('SearchCtrl', { $scope: $scope, StudentsProvider: mockStudentsProvider}); 
      StudentsProvider = mockStudentsProvider; 
     })); 
     describe("Update", function() { 
      beforeEach(function() { 
       spyOn(StudentsProvider, 'getStudents'); 
      }); 
      it("should always call the provider with 3 letters", function() { 
       $scope.keyword = "axe"; 
       $scope.update(); 
       expect(StudentsProvider.getStudents).toHaveBeenCalled(); 
       expect(StudentsProvider.getStudents).toHaveBeenCalledWith("axe", ""); 
      }); 
     }); 
    }); 
}); 

當我運行它,我得到以下錯誤:

TypeError: 'undefined' is not an object (evaluating 'StudentsProvider.getStudents($scope.keyword, $scope.selectedFilters).success') 

,它的可能是因爲我沒有嘲笑.success()回調。我會怎麼做?提前致謝!

回答

7

替換此:

var mockStudentsProvider = { 

    getStudents: function getStudents() { 
     return [{ 
      Education: [], 
      Person: [{ 
       ID: 1, 
       Name: "Testing McTestsson", 
       SSN: "1234567890", 
       Address: "Fakestreet 3", 
       MobilePhone: "7777777" 
      }] 
     }]; 
    } 
}; 

與此:

var mockStudentsProvider = { 
    getStudents: function getStudents() { 
     var retVal = [{ 
      Education: [], 
      Person: [{ 
       ID: 1, 
       Name: "Testing McTestsson", 
       SSN: "1234567890", 
       Address: "Fakestreet 3", 
       MobilePhone: "7777777" 
      }] 
     }]; 
     return { 
      success: function(fn) { 
       fn(retVal) 
      }; 

     } 
    } 
}; 

而且替換此:

spyOn(StudentsProvider, 'getStudents'); 

與此:

spyOn(StudentsProvider, 'getStudents').andCallThrough(); 
  1. 當你不使用andCallThrough()andCallFake() jasmine阻止執行該方法並返回null。在您的更新方法中,您正在調用null.success。這將失敗。 (http://jasmine.github.io/1.3/introduction.html

  2. 在你的模擬方法,你需要改變返回格式 - 在那裏,成功是指需要輸入一個回調函數的函數真正的HTTP方法返回一個對象。

在你的情況下,回調函數是:

function(data) { 
    $scope.students = data; 
} 
相關問題