Hello, this is dayossi of iOS engineers.
I want to provide a service that makes my family happy We have released a family diary app called HaloHalo.
This time, about the unit test I would like to sort out the prerequisites that I did not understand well.
Even when the app is running A technique for continuously checking if your code works.
Debugging with print () allows you to check single processing results and variable values, If there is a similar process in other situations, You need to write print () again.
Also, if the same process appears many times, It is better to manage it in a unified format so that it can be used repeatedly. It's more readable and it's clear what you're testing.
Unit tests are done under a unified format The result of repeated processing can be automatically and repeatedly obtained. It seems that it is a tool that can be made.
(Reference books: iOS test complete book (2019) from Chapter 1)
This is the main subject.
I understand the benefits of unit testing, On the contrary, "If you can't write in a design that can call the process repeatedly, you can't test." It seems that it can be considered.
I didn't really understand this point, so Below, we will organize them in order.
For example, a process in a particular class If you instantiate the class every time and call the process.
This is a class-dependent case, The process and class are not separated.
As shown below, sortAscendData () described in the view controller If you want to use it with OtherViewCotroller
At this rate, you can instantiate the view controller and call it. It takes time to write the same code in OtherViewCotroller.
If many similar efforts occur, the amount of code to write will increase wastefully, When checking the behavior of the entire app, it becomes difficult to understand where the bug occurred.
//Referenced view controller
import UIKit
class ViewController: UIViewController {
var userDataCollection:[String] = []
override func viewDidLoad() {
super.viewDidLoad()
//View drawing
setupView()
}
func setupView(){
//Draw View
self.view.addSubview(<#T##view: UIView##UIView#>)
}
func getUserData(){
//User data acquisition process
}
func reloadView(){
//View redraw
}
func didTacAction(){
//Detection of tap motion
}
func sortAscendData(_ userDataCollection:Array<String>){
//Data sorting process
}
}
//ViewController you want to reference
class OtherViewController: UIViewController {
//Display data is stored and I want to sort...
var otherUserData: [String] = []
let vc = ViewController()
vc.sortAscendData(self.otherUserData)
// MARK:-All you really need is this...
func sortAscendData(_ userDataCollection:Array<String>){
//Data sorting process
}
}
The common process used in various situations is If you abstract it with protocol,
Without depending on a specific class Also, it becomes clear that there is a common process in the protocol. It will be easier to check the behavior of the entire app.
Therefore, if the responsibilities cannot be shared for each file Since it becomes difficult to extract the process you want to test,
First of all, design that can clarify "where and what kind of responsibility do you have?" I think it's important to think about it.
Being aware of design means It can be said that it is clear which role is managed by which file.
I think that the number of files to manage will inevitably increase, It makes it easier to do both automatic verification by the test unit and visual check. I think it will be effective even in situations where you are aware of development speed.
I will write more and more unit tests!
(I would appreciate it if you could give me a warm tsukkomi such as "Isn't it okay to think this way here?")
Kazuaki Matsuo, Yusuke Hosonuma, Kenji Tanaka et al. IOS Test Complete Book (2019) Introduction to Swift Unit Testing IOS application development policy for designing at the same time while writing code I tried to make an iOS application with MVC now (Swift) / Kai
Recommended Posts