最近,我们开始为我们的应用程序编写测试用例,我们需要帮助为控制器编写测试用例。我们使用Mocha、chai和Snion库来编写测试用例。
下面是柱塞链接,其中包含我们的控制器代码。有人能帮助我们如何为这个控制器编写测试用例吗?我们将在此基础上为其余部分实现。我们需要这个控制器的初始推力。
http://plnkr.co/edit/oginuqO0afxnWbVMos0f?p=info
以下是代码:
angular.module( 'ngBoilerplate.account', [
'ui.router','ngAnimate', 'ui.bootstrap','ngBoilerplate.contact','ngResource','jcs-autoValidate','ngCookies','ngTagsInput'
])
.controller('addAccount', function($scope,industryService,$http,$state,LoggedUser){
$scope.industry = [];
industryService.query().$promise.then(function(data) {
$scope.industry = data;
});
window.onbeforeunload = function (event) {
if ($scope.addAccountForm.$dirty) {
var message = 'If you leave this page you are going to lose all the unsaved changes.';
if (typeof event == 'undefined') {
event = window.event;
}
if (event) {
event.returnValue = message;
}
return message;
}
};
$scope.insertAccount = function(){
$scope.address = {
'line1':$scope.line1,
'line2':$scope.line2,
'city':$scope.city,
'zipCode':$scope.zipCode,
'state':$scope.state,
'country':$scope.country
};
console.log($scope.industryId);
if($scope.industryId!== undefined) {
$scope.industry = {
'id' : $scope.industryId
};
}
$http.post('/rest/users/'+LoggedUser.getUserName()+'/accounts',{
'name' : $scope.name,
'industryBean': $scope.industry,
'email' :$scope.email,
'phone' : $scope.phone,
'fax' : $scope.fax,
'website' : $scope.website,
'headquarters' : $scope.headquarters,
'dbaName' : $scope.dbaName,
'numberOfEmployees' : $scope.numberOfEmployees,
'annualRevenue':$scope.annualRevenue,
'logo' : $scope.logo,
'primaryContact': $scope.contact,
'addressBean':$scope.address
}).success(function(data){
$scope.account=data;
$state.go('main.account', {}, {reload: true});
});
};
})
.factory("loggedInUser", function($resource) {
return $resource("/rest/users/:username");
})
.factory("industryService", function($resource) {
return $resource("/rest/accounts/industry");
})任何帮助都是非常感谢的。
谢谢,如果您有同样的问题,请告诉我。
发布于 2015-12-23 11:42:22
我喜欢mocha chai和Sinon,并将它们用于测试节点代码。从没用过有棱角的。
典型的角度设置是卡玛,茉莉花(单元测试)和量角器(E2E测试)。
看看您的控制器代码,我会说控制器中有太多的逻辑。您需要将一些代码降级到服务中。
在测试$http方面,需要使用ng-mock的$httpBackend。
您应该在代码中也使用角控制器作为语法。
https://stackoverflow.com/questions/34430496
复制相似问题