最近前后端代码写完了,研究下angularjs单元测试,网上找了好多资料,都是一知半解,很散,为了记录下痛苦的学习历程和为即将要学习的战友提供点帮助,决定写一下。
$scope.getFormList = function(){
var name = $scope.selName === undefined ? "" : ("&name=" + $scope.selName);
var selStart = "?start=" + dbCur + "&limit=" + $scope.itemsPerPage;
formsService.formList(selStart+name).success(function(data) {
$scope.gridOptions.data = data.data;
$scope.totalCount = data.total;
$scope.pageCount = Math.ceil($scope.totalCount / $scope.itemsPerPage) - 1;
}).error(function(data) {
$scope.errordata = data;
});
};
angular.module('inspinia').factory('formsService', ['$http', 'baseUrl',function($http, baseUrl){
return {
formList: function(param) { //获取表单列表
var url = baseUrl + 'api/forms' + param;
return $http({
method: 'GET',
url: url });
},
}]);
beforeEach(inject(function (_formsService_, $httpBackend){
formsService = _formsService_;
httpBackend = $httpBackend;
//下面这个语句:如果service调用了这个url,那么将会返回{"data":[{"_id":{"$oid":"57286b306d61a3fc61c58de5"},"name":"ggg"}],"total":1}
httpBackend.whenGET( 'http://localhost:9000/api/forms?start=1&limit=100000')
.respond(200, '{"data":[{"_id":{"$oid":"57286b306d61a3fc61c58de5"},"name":"ggg"}],"total":1}');
}));
it('should get a form from mongo', function(){
expect(httpBackend).not.toEqual(null);
$scope.getFormList();
httpBackend.flush();
expect($scope.totalCount).toEqual(1);
})
研究了一天,转变了思路,觉得自己想复杂了,还是要多看官方文档,下面贴出整个测试代码:
describe('inspinia controllers', function() {
var scope,ctrl, formsService;
var http, baseUrl, httpBackend;
var $scope = {}
beforeEach(module('inspinia'));
beforeEach(function() {
module('inspinia', function($provide) {
baseUrl = $provide.constant('baseUrl', 'http://localhost:9000/'); // <= mock your constant
});
});
beforeEach(inject(function (_formsService_, $httpBackend){
formsService = _formsService_;
httpBackend = $httpBackend;
httpBackend.whenGET( 'http://localhost:9000/api/forms?start=1&limit=100000')
.respond(200, '{"data":[{"_id":{"$oid":"57286b306d61a3fc61c58de5"},"name":"ggg"}],"total":1}');
}));
beforeEach(inject(function ($rootScope, $controller) {
// scope = $rootScope.$new();
ctrl = $controller('formsCtrl', {$scope: $scope, formsService: formsService});
}));
it('begin init...', function(){
$scope.init();
expect($scope.itemsPerPage).toEqual(100000);
expect($scope.currentPage).toEqual(0);
expect($scope.totalCount).toEqual(0);
expect($scope.selected).toEqual([]);
expect($scope.add).toEqual({});
})
it('get sheetList', function(){
$scope.getStyleSheetList();
expect($scope.add.styleSheets).toEqual("传统表单");
})
it('click modify get data', function(){
var kv = {"name":"1"};
$scope.getUpd(kv);
expect($scope.upd.name).toEqual("1");
})
it('should have an initial documentSaved state', function(){
expect($scope.itemsPerPage).toEqual(100000);
expect($scope.currentPage).toEqual(0);
expect($scope.totalCount).toEqual(0);
});
it('click add button to clear data', function(){
$scope.clearAddInfo(null);
expect($scope.add.style).toEqual("申请表单");
expect($scope.add.styleSheets).toEqual("传统表单");
});
it('should have a properly woking formsCtrl controller', function(){
expect(ctrl).not.toEqual(null);
})
it('should have a properly working formsService service', function(){
expect(formsService).not.toEqual(null);
})
it('should get a form from mongo', function(){
expect(httpBackend).not.toEqual(null);
$scope.getFormList();
httpBackend.flush();
expect($scope.totalCount).toEqual(1);
})
});