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()回调。我会怎么做?提前致谢!