您好我有这项服务:
angular.module('angularProtoApp')
.factory('ModelService',['$q', '$http', function ($q, $http) {
var folder = 'assets/';
var extension = 'Model.json';
return {
getModelFromJson : function (pageCode) {
return $http({
method : 'GET',
url : folder + pageCode + extension,
headers: {'Content-Type': 'application/json'}
}).then(
function (response) {
return response.data;
}
);
}
};
}]);
我有这个控制器:
angular.module('angularProtoApp')
.controller('MainCtrl', ['$scope', '$location', 'ModelService', function ($scope, $location, ModelService) {
$scope.model = '';
ModelService.getModelFromJson('Ffco').then(
function (data) {
$scope.model = data;
if ($scope.model.isSeleniumTestRunning) {
$scope.slnPageCodeClass = 'sln' + $scope.model.pageCode;
}
$location.path('/' + $scope.model.pageCode);
},
function (error){
console.log(error);
}
);
}]);
但我不知道如何正确测试控制器。 我开始做这样的事情,但我认为这还不够。
describe('Controller: MainCtrl', function () {
// load the controller's module
beforeEach(module('angularProtoApp'));
var MainCtrl,
scope,
ModelServiceMock;
beforeEach(inject(function ($controller, $rootScope, _ModelService_) {
scope = $rootScope.$new();
ModelServiceMock = _ModelService_;
spyOn(ModelServiceMock, 'getModelFromJson').and.callThrough();
$controller('MainCtrl', {
$scope: scope,
ModelService : ModelServiceMock
});
}));
it('should call getModelFromJson method on ModelService', function () {
scope.$digest();
expect(ModelServiceMock.getModelFromJson).toHaveBeenCalled();
});
});
我不确定我是否正确测试了服务,但我没有成功测试控制器的范围变量。
有人知道如何正确测试吗?
感谢。