mardi 6 août 2019

How to mock and test a UserDefaults computed properties that store String?

I am trying to mock UserDefaults to be able to test its behaviour.

What is the best way to do it without corrupting the real computed property that save the user token key?

class UserDefaultsService {

  private struct Keys {
    static let token = Key.partageTokenKey.rawValue
  }

  //MARK: - Save or retrieve the user token
  static var token: String? {
    get {
      return UserDefaults.standard.string(
        forKey: Keys.token)
    }
    set {
      UserDefaults.standard.set(
        newValue, forKey: Keys.token)
    }
  }
}

Aucun commentaire:

Enregistrer un commentaire