How can I use unit test my view controllers (subclasses of UIViewController) in an iOS application?
I have searched this topic many times over the past few days, but I still cannot figure out what the correct way is.
It seems that there are at least three ways of a logical controller testing controller:
Open the personal actions and IBOutlets that you want to use unit test (declare them in the header file).
Use a category, such as Tests , to access private activities and IBOutlets from unit tests.
Do not expose anything, instead find buttons and other views by name or other public property and simulate user interaction using public UIView methods (for example, simulate a user by clicking a button); then observe the apparent state.
I have not found many sources on this, but there is an example on objc.io.
Now, to be honest, I donβt like the first two, because, as I understand it, unit tests should not check the internal objects of an object (i.e. private methods) and declare them public only for the sake of tests, It seems to be the best practice. I usually keep IBActions and IBOutlets inside the implementation, but now I suddenly need to make everything public just because I add tests ...
I think there may be another alternative: move as much logic as I can from my view controllers to independent components and test them (leaving the controllers unchecked or mostly unchecked). This seems like a good idea, but I will have to do a lot of refactoring (I am currently adding unit tests for a project that has not been coded for testing).
So, I was wondering what is the best way to test view controllers?
source share