skip to Main Content

i want to check if a collection is sorted in my UnitTest using XCTest, does swift iOS provide any such framework or method.

let countrieds = ["Alabama","Benin","Alaska","Togo"]
    XCTAssertTrue(countrieds.sorted())

The above is just a sample and ofcourse i get the error below

Cannot convert value of type ‘[String]’ to expected argument type ‘Bool’

i will be glad to be educated on how to use XCTest to determine if a collection is sorted.

2

Answers


  1. Use

    XCTAssertTrue(countrieds == countrieds.sorted())
    

    or

    XCTAssertEqual(countrieds, countrieds.sorted())
    
    Login or Signup to reply.
  2. A simple way to test that is to do an assertEqual with your array.
    For exemple:

    let countries = ["Alabama","Benin","Alaska","Togo"]
    XCTAssertEqual(countries, countries.sorted())
    

    (In this exemple the test will return false obviously)

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search