1

I have a need to add a library to an app, the trouble is the library is using PromiseKit. My application does not use promises, as such I was planning on introducing an adapter to map the promise results to completion handlers.

What I am unsure of is, when unit testing this, I will need to create a spy, but this means being able to, or at least stub, the behaviour of a promise returning something.

I had considered adding PromiseKit as a dependency to my test target only, but not sure if this is the best approach as I don't actually care about real promises.

An example would be :-

protocol OAuthAdapterType: SessionStateProviderType { }

final class OAuthAdapter: OAuthAdapterType {

  private weak var oauthProvider: OAuthProviderType?

  init(oauthProvider: OAuthProviderType?) {
    self.oauthProvider = oauthProvider
  }
}

extension OAuthAdapter: SessionStateProviderType {

  func getStatus(then completion: @escaping (Result<SessionState, Error>) -> Void) {
    oauthProvider?.checkSession()
      .done { completion(.success(SessionStateMapper.map($0))) }
      .catch { _ in completion(.success(.signedOut)) }
  }

}

final class SessionStateMapper {
  static func map(_ status: Bool) -> SessionState {
    return status ? .signedIn : .signedOut
  }
}

In this case, checkSession is a Promise<Bool>.

I have created a Spy would looks something like :-

import XCTest
import PromiseKit
import OAuthKit

class OAuthProviderSpy: OAuthProviderType {
  var invokedCheckSession = false
  var invokedCheckSessionCount = 0
  var stubbedCheckSessionResult: Promise<Bool> = .value(false)
  func checkSession() -> Promise<Bool> {
    invokedCheckSession = true
    invokedCheckSessionCount += 1
    return stubbedCheckSessionResult
  }
}
Harry Blue
  • 4,202
  • 10
  • 39
  • 78

0 Answers0