【发布时间】:2016-10-15 06:23:51
【问题描述】:
嘿,我是 AngularJS 测试的新手,我想弄清楚,
我应该如何测试负责我的休息调用的 AngularJS 服务。
如何在我要测试的其他控制器中调用此服务。
我需要测试使用休息请求的数据工厂服务 需要测试的代码是这样的:
var app = angular.module("app",[]);
app.controller("mainCTRL", ["$scope","dataFactory",function($scope,dataFactory){
$scope.title = "Hello World";
dataFactory.getEntries("fakeSuffix");
}]);
app.factory('dataFactory', ['$http', '$window', '$log', function ($http, $window, $log) {
var urlBase = $window.location.origin + '/api',
dataFactory = {};
/**
* get all Entries.
**/
dataFactory.getEntries = function (suffix) {
$log.debug("************ Get All Entries ************");
$log.debug("url:", urlBase + suffix);
return $http.get(urlBase + suffix, { headers: { cache: false } });
};
/**
* get single Entry.
**/
dataFactory.getEntry = function (id) {
$log.debug("************ Get Single Entry ************");
return $http.get(urlBase + '/' + id);
};
/**
* insert Entry
**/
dataFactory.postEntry = function (method, entry) {
var url = urlBase + '/' + method;
return $http.post(url, entry);
};
/**
* update Entry
**/
dataFactory.updateEntry = function (entry) {
$log.debug("************ Update Single Entry ************");
return $http.put(urlBase + '/' + entry.id, entry);
};
/**
* delete Entry
**/
dataFactory.deleteEntry = function (id) {
$log.debug("************ Delete Single Entry ************");
return $http.delete(urlBase + '/' + id);
};
return dataFactory;
}]);
<script src="https://ajax.googleapis.com/ajax/libs/jQuery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div id="block" ng-app="app" ng-controller="mainCTRL">
{{title}}
</div>
【问题讨论】:
标签: javascript angularjs rest mocking jasmine