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()回調。我會怎麼做?提前致謝!