Reputation: 253
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 = "";
}
}
Upvotes: 1
Views: 39
Reputation: 222494
It always depends on the case, but yes, it is generally a good idea to expose private class methods.
Private methods can be named with _
prefix convention to distinguish them from public methods and optionally be made non-enumerable with Object.defineProperty
.
JavaScript cannot reflect private members (local variables). Besides being the one of fundamental OOP principles and 'a good thing' in general, the encapsulation damages testability in JS and has little to nothing to offer in return.
Encapsulation doesn't add security to JS application, and treating a unit as a blackbox doesn't help anything. Putting spies on every called method helps, on the other hand.
Upvotes: 0
Reputation: 6269
Definitely not a good idea to expose private methods just for the sake of unit testing.
You should be able to test what those methods are doing by expecting a specific output.
For example:
Upvotes: 1