dimanche 27 septembre 2015

How should I test methods that call other methods in swift

I have the following function that I would like to write tests for.

  func makeMarkerRequest(size: Int) {
    let markerRequestCommand = commandFactory.createGetMapMarkersCommandUsing(apiRepo) {
      if let returnedMarkers = $0.swiftObject as? MapMarkers {
        if let returnedMarkerList = returnedMarkers.markers {
          let markerChanges = self.getMarkerChanges(returnedMarkerList)
          print("marker changes = \(markerChanges)")
          if markerChanges.markersToBeAdded.count > 0  { self.addMarkers(markerChanges.markersToBeAdded, size: size) }
          if markerChanges.markersToBeMoved.count > 0  { self.moveMarkers(markerChanges.markersToBeMoved) }
          if markerChanges.markersToBeRemoved.count > 0  { self.removeMarkers(markerChanges.markersToBeRemoved) }
        }
      }
    }
    commandProcessor.processCommand(markerRequestCommand, retryProcessor: commandProcessor)
  }

Ultimately this function goes off and retrieves some data from a server using Alamofire. I already have separate tests for that as well as the getMarkerChanges, addMarkers, moveMarkers, and removeMarkers methods of this class, so when I test this method, I think I only really want to test that the right data gets passed to the right methods under the correct conditions.

I am familiar with writing mocks for dependancies, so I already have a mock for the commandFactory, which returns a mock command. Do I handle the getMarkerChanges, addMarkers, etc, the same way?

I am tempted to write a mock of the object I want to test, and just override those particular methods and then just test that the method is called with the right parameters.

Is there anything wrong with that approach? Is there a more established way to handle these case?

Aucun commentaire:

Enregistrer un commentaire