Marco Santarossa Marco Santarossa - 3 years ago 182
Swift Question

How to mock #available with Swift?

I have a Swift class which computes something just for iOS 11. This means that I have the following check:

if #available(iOS 11, *) {
//do magic
}


I'm wondering how I can mock
#available
for my unit tests. I would like to test:


  1. If
    #available
    is called with the right parameters

  2. The code inside the if statement. For it, I would like having a way to force the result of the statement of
    if #available(iOS 11, *)
    .



Update:

Since I had these difficulties, I changed approached and moved to:

let result = device.systemVersion.compare("11", options: .numeric)
return result == .orderedSame || result == .orderedDescending


Any answers to the problem are greatly appreciated!

Answer Source

Since I had these difficulties, I changed approached and moved to:

let result = device.systemVersion.compare("11", options: .numeric)
return result == .orderedSame || result == .orderedDescending
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download