Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check is variable is array or not with XCTest

Do how to know if variable return function is an array or not ? Example: In my Presenter I have this function:

 func filterGnomosForName(name:String) -> [Gnomo]{
    let res = listGnomos.filter { ($0.name?.lowercased().contains(name.lowercased()))!}
    return res
}

And MyTestClass I have this test function:

    func testFilterGnomo(){
    let result = listPresenter.filterGnomosForName(name: "Nam")
    XCTAssert(result == [Gnomo])
}

Gnomo is a type object in array , but only want know if is array or not for know if the funcion is correctly or not , help me?

like image 584
Alejandro Avatar asked Dec 06 '25 09:12

Alejandro


1 Answers

When I am asserting an objects type I do this:

XCTAssert((object as Any) is Array)

I've not asserted an array before but I think the above would work.

Here is an example I just created in a Swift Playground:

import UIKit
import XCTest

class MyTestClass: XCTestCase {

    func testSomething() {
        let myArray: [String] = ["foo", "bar"]

        XCTAssert((myArray as Any) is String) // Fails
        XCTAssert((myArray as Any) is [String]) // Passes
    }
}

MyTestClass.defaultTestSuite().run()
like image 159
Hodson Avatar answered Dec 07 '25 21:12

Hodson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!