I'm wondering what the best way is to write unit tests for private functions and properties in classes. Say for example, I have a class that represents a ball launched as a projectile:
class ball %
properties (Access = private) initialSpeed = 25; %m/s
acceleration = -10; %m/s^2
end % methods function distTravelled = getDistanceTravelled(obj, timeElapsed) % Estimate total distance traveled by the ball
% (I know this is a bad approximation)
currSpeed = obj.getCurrSpeed(timeElapsed); distTravelled = 0.5 * (obj.initialSpeed + currSpeed) * timeElapsed; end end % methods (Access = private) function currSpeed = getCurrSpeed(obj, timeElapsed) %Calculate current speed of ball
currSpeed = obj.initialSpeed + obj.acceleration * timeElapsed; end endend
How should I write a test to check that the values for acceleration or that the value returned by the method getCurrSpeed is accurate? Should I just allow access to the testing functions?
Best Answer