I'm struggling to understand how should I write the methods for the Angular.js controllers to allow the unit testing to be done. All the tutorials on the Internet are simple and don't show how it is looks like in the real life applications. Those tutorials are showing that all the methods are exposed by attaching them to "scope" or to "this (aka vm)". In my understanding, methods that won't be used outside of the controller don't need to be exposed. In the example below I'm exposing only two methods since they are triggered by clicking the buttons on the page. The rest of the methods merely serve for internal purposes. How can I test the private methods in the controller without exposing them? Do I need to expose all of them to be able to unit test? Is it a good practice to expose all the methods? Thank you
angular.module('app.pool',[])
.controller('PoolController', PoolController);
function PoolController(PoolService) {
var vm = this;
vm.candidateName='';
vm.candidatePicUrl='';
vm.approveCandidate = approveCandidate;
vm.refuseCandidate = refuseCandidate;
function approveCandidate() {
PoolService.approveCandidate();
getNextCandidate();
}
function refuseCandidate() {
PoolService.refuseCandidate();
getNextCandidate();
}
function getNextCandidate() {
clearProfile();
PoolService.getNextCandidate().
success(displayUserData);
}
function displayUserData(data) {
vm.candidateName = getCandidateName(data);
vm.candidatePicUrl = getCandidateProfilePic(data);
}
function getCandidateName(data) {
return data.userName;
}
function getCandidateProfilePic(data) {
return changeUrlToBiggerPic(data.profilePicture);
}
function changeUrlToBiggerPic(url) {
return url.replace('s150x150', 's600x600');
}
function clearProfile() {
vm.candidateName = "";
vm.candidatePicUrl = "";
}
}
Aucun commentaire:
Enregistrer un commentaire