【发布时间】:2017-01-01 11:51:07
【问题描述】:
谁能告诉我在我的控制器函数 getData 和工厂函数上运行测试的最佳方法。我很困惑,不知道从哪里开始。您将如何为下面的代码编写测试?
myApp.controller('myController', ['$scope', 'myFactory', function ($scope, myFactory) {
$scope.getData = function(id) {
var promise = myFactory.GetData('/dta/GetData?Id=' + id);
promise
.then(function (success) {
$scope.result = success;
}, function (error) {
$scope.error = true;
});
}
});
myApp.factory('myFactory', ['$http', function ($http) {
return {
GetData: function (url) {
return $http.get(url)
.then(function (response) {
return response.data;
}, function (error) {
return error;
});
}
}
}]);
【问题讨论】:
-
你想测试什么,你的控制器的
getData方法还是你工厂的GetData方法? -
...对于前者,您可以使用create a mock (spy) of
myFactory。对于后者,使用$httpBackend -
我想测试上面看到的所有代码。我不确定有经验的测试人员会做什么。
-
在测试控制器的getData函数时,我是不是也使用了$httpBackend?
标签: angularjs unit-testing karma-jasmine