mercredi 7 octobre 2015

Swift 2: can Protocol-Oriented Programming save me from having to make a mock GKMatch?

The new Protocol-Oriented features in Swift 2 were introduced at WWDC with much ballyhoo, including the claim "it also saves us from having to make mocks all the time".

That sounds great--I'd love to be able to write tests without mocks.

So I set up a nice protocol/extension pair for GKMatch, like so:

protocol SendData {
  func send(data: NSData)
}

extension GKMatch: SendData {
  func send(data: NSData) {
    do {
      try self.sendData(data, toPlayers: self.players,
        dataMode: .Reliable)
    } catch {
      print("sendData failed with message: \(error)")
    }
  }
}

//Now what? How to test without a GKMatch or mock GKMatch?

Since GKMatch can't be instantiated directly, in order to do tests in the previous version of Swift I had to build a mock GKMatchmaker that would return a mock GKMatch and it was a whole complicated thing. That's why my ears perked up at that line in the WWDC talk.

But if the protocol-oriented approach is enabling freedom from mocks here, I'm not seeing it.

Can anyone show me how I'd test this code without making a mock?

Aucun commentaire:

Enregistrer un commentaire