Skip to content

Instantly share code, notes, and snippets.

@kittisak-phetrungnapha
Created January 10, 2019 09:46
Show Gist options
  • Save kittisak-phetrungnapha/66cdfd860cca3614764d6f2a43b74ddf to your computer and use it in GitHub Desktop.
Save kittisak-phetrungnapha/66cdfd860cca3614764d6f2a43b74ddf to your computer and use it in GitHub Desktop.
class NetworkTest: XCTestCase {
func testGetContacts() {
let expectation = self.expectation(description: "contacts")
var contacts: [Contact]?
Network.shared.request(router: Router.getContacts) { (result: Result<[Contact]>) in
switch result {
case .success(let contactList):
contacts = contactList
default:
break
}
expectation.fulfill()
}
waitForExpectations(timeout: 10, handler: nil)
XCTAssertNotNil(contacts)
}
func testGetContactWithId() {
let id = 2454
let expectation = self.expectation(description: "contact")
var contact: Contact?
Network.shared.request(router: Router.getContact(id: id)) { (result: Result<Contact>) in
switch result {
case .success(let item):
contact = item
default:
break
}
expectation.fulfill()
}
waitForExpectations(timeout: 10, handler: nil)
XCTAssertEqual(contact?.id, id)
}
func testCreateNewContact() {
let expectation = self.expectation(description: "create_contact")
let params: [String: Any] = [
"first_name": "Amitabh2",
"last_name": "Bachchan2",
"email": "[email protected]",
"phone_number": "+919980123412",
"favorite": false
]
var contact: Contact?
Network.shared.request(router: Router.createContact(body: params)) { (result: Result<Contact>) in
switch result {
case .success(let item):
contact = item
default:
break
}
expectation.fulfill()
}
waitForExpectations(timeout: 10, handler: nil)
XCTAssertNotNil(contact?.firstName, "Amitabh2")
}
func testUpdateContact() {
let id = 2454
let expectation = self.expectation(description: "update_contact")
let params: [String: Any] = [
"first_name": "Updated2",
"last_name": "Name2"
]
var contact: Contact?
Network.shared.request(router: Router.updateContact(id: id, body: params)) { (result: Result<Contact>) in
switch result {
case .success(let item):
contact = item
default:
break
}
expectation.fulfill()
}
waitForExpectations(timeout: 10, handler: nil)
XCTAssertEqual(contact?.id, id)
XCTAssertEqual(contact?.firstName, "Updated2")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment