Unit test for a password-change request
Some hints on Unit tests. 


Use dependency injection to make testing easier
Create mocks to eliminate all external dependencies in tests
Test units of code (functions, methods, classes)


Example of testable class:



class UserManager: UserManagerType {
 let database: DatabaseType
 init(database: DatabaseType) {
  self.database = database
 }

 func getUser(withId id: String) -&gt; User? {
    return database.getItem(withPredicate: "id == \(id)")
 }

 func getAdmins() -&gt; [User] {
    return database.getItems(withPredicate: "type == admin")
 }
}

protocol DatabaseType { 
   func getItems(withPredicate predicate: String) -&gt; [User]
   func getItem(withPredicate predicate: String) -&gt; User?
} 


Tests: 



// create mock for database.
// Try to avoid any external dependencies in tests
class DatabaseMock: DatabaseType {
 var itemsToReturn: [User] = []
 var userToReturn: User?

 func getItems(withPredicate predicate: String) -&gt; [User] {
   return itemsToReturn
 }

 func getItem(withPredicate predicate: String) -&gt; User? {
   return userToReturn
 }
}

// actual test case
class UserManagerTests: XCTestCase {
  var database: DatabaseMock = DatabaseMock()
  var manager: UserManager!
  // setup before each test
  func setUp() {
    database = DatabaseMock()
    manager = UserManager(database: database)
  }

  func testReturnsCorrectUserForId() {
    let expectedUser = User(id: "123")
    database.userToReturn = expectedUser
    let actualUser = manager.getUser(withId: "123")
    XCTAssertNotNil(actualUser)
    XCTAsseerEqual(actualUser.id, expectedUser.id)
  }

  // other tests ...
}


This is not the best example (because UserManager is just a wrapper around database) but I hope you have got some ideas of how write unit tests