In this session, we will be focusing on data modeling. So there will be no UI construction in this session.
However, we still need to create a project anyway. So lets get starting. Create a single view application project.
Named the project as "Intro14".
We can ignore the storyboard and create a new Swift file.
Select Swift File.
Named the Swift file as "AddressBook.swift"
Once the file is created, it will be an empty file with some comment and an import statement. We will add to the file a new class called AddressBook. At the same time, we can setup the properties section and initialization section.
Now lets add data structure to the properties section.
// MARK: Properties
var name: String
var address1: String
var address2: String
var address3: String
var zipcode: Int
var phoneNumber: String
Next we need to initialized all the variable in the initialization section. In this data model, we only required that the name must not be empty, therefore, we need to perform check to see if name is empty. The initialization will not be continue. In this case we will be using init? instead of init.
// MARK: Initialization
init?(name:String, address1:String, address2:String, address3:String, zipcode:Int, phoneNumber:String) {
self.name = name
self.address1 = address1
self.address2 = address2
self.address3 = address3
self.zipcode = zipcode
self.phoneNumber = phoneNumber
Afther we have initialized each variable, then we need check if name is empty. If it is then we will not create the object and return nil.
if name.isEmpty {
return nil
}
The code for the Swift file is as follows:
import Foundation
class AddressBook {
// MARK: Properties
var name: String
var address1: String
var address2: String
var address3: String
var zipcode: Int
var phoneNumber: String
// MARK: Initialization
init?(name:String, address1:String, address2:String, address3:String, zipcode:Int, phoneNumber:String) {
self.name = name
self.address1 = address1
self.address2 = address2
self.address3 = address3
self.zipcode = zipcode
self.phoneNumber = phoneNumber
if name.isEmpty {
return nil
}
}
}
Next, we will perform some testing for our data model. Select Intro14Tests.swift as shown below:
You will see some template code in the main class Intro14Tests: XCTestCase.
Delete everything inside the class and add the following code:
func testAddressBookInitialization() {
let goodCase = AddressBook(name:"Thomas", address1:"MyAddress", address2:"", address3:"", zipcode:523973, phoneNumber:"212-35423445")
XCTAssertNotNil(goodCase)
}
Run a test buy clicking on the diamond next to func testAddressBookInitialization
If the test successful, there should be a tick as shown below:
If there is any problem, recheck the code again. Next we will add another 2 test case as follows:
let badCase = AddressBook(name:"", address1:"", address2:"", address3:"", zipcode:0, phoneNumber:"")
XCTAssertNil(badCase, "Name is essential")
let wrongCase = AddressBook(name:"", address1:"MyAddress1", address2:"MyAddress2", address3:"", zipcode:123432, phoneNumber:"212-2343556")
XCTAssertNil(wrongCase)
The whole code is as follows:
import XCTest
@testable import Intro14
class Intro14Tests: XCTestCase {
func testAddressBookInitialization() {
let goodCase = AddressBook(name:"Thomas", address1:"MyAddress", address2:"", address3:"", zipcode:523973, phoneNumber:"212-35423445")
XCTAssertNotNil(goodCase)
let badCase = AddressBook(name:"", address1:"", address2:"", address3:"", zipcode:0, phoneNumber:"")
XCTAssertNil(badCase, "Name is essential")
let wrongCase = AddressBook(name:"", address1:"MyAddress1", address2:"MyAddress2", address3:"", zipcode:123432, phoneNumber:"212-2343556")
XCTAssertNil(wrongCase)
}
}
Now, run the test again. Click on the diamond as shown below:
There should be one error and it is intentional. The error message is shown below:
The test cases we are using is to test if error will occur when instances/object are created from the data model class. In the first case, we filled up everything properly, so there should be no mistake. In the second case, we intentionally left out the name. Therefore the class should return nil and we use XCTAssertNil. The test case is correct and assertion is also correct. For the last case, we also left out the name, however, this time we use XCTAssertNotNil. This generate error as we suppose to use XCTAssertNil instead.
To correct the mistake, change the last assertion to XCTAssertNil as shown below:
Now, try to run the test again. We should be getting a tick as shown below:
No comments:
Post a Comment