Learn the basics of constructing unit tests and UI tests, and how to incorporate them into your app.
This is a companion discussion topic for the original entry at https://www.raywenderlich.com/251-updated-course-testing-in-ios
Learn the basics of constructing unit tests and UI tests, and how to incorporate them into your app.
Do you have suggestions for testing private functions? The @testable let us access all the āinternalā stuff, but not āprivateā. For example, the class might have a private method that implements some very complex algorithm. I of course want to unit test that algorithm but I canāt access it. It stinks that I end up making methods āinternalā just so I can test them when I really donāt want them accessible by any other class.
One of the reasons for introducing the internal
access modifier in Swift, if Iām not mistaken, was to allow testing of methods which were previously marked private
. So your solution for testing private methods would be to mark them as internal
Since internal
allows access from within only the current app or module, Iām not sure why it would be an issue to mark items as internal
instead of private
?
Theyāre really very different things. internal
makes it available to the entire project, which is definitely not what is wanted here. The method should be private
so that nothing accidentally calls that or thinks itās available for internal use.
I agree that they are different - thatās why you have two different attributes But as a developer, I see private
working differently than you do. I donāt think in terms of āaccidentalā use. I should know what Iām accessing and not accessing in my own codebase. So Iām mostly concerned with how others access or arenāt able to access certain properties or methods in my classes. So, for general work, private
and internal
work just fine to fence out relevant bits for testing (from within my own project) vs. not allowing access to some items at all from external libraries/code.
As far as what you are looking for goes, to test private methods using unit testing, Iām afraid that thatās not possible and goes against the intents of the private
attribute
Thatās a great assumption, as long as itās not a team of people working on it.
But you clearly answered my question. If I want to be able to test it, then I have to make it internal. Thanks.
This topic was automatically closed after 166 days. New replies are no longer allowed.