I'm trying to write Unit Tests, and I'm currently trying to check the viewController that got the data in viewDidLoad(), the data is set after an alamofire request. The problem is that in my test function, when I check the array, which should be filled with 10 elements after a successful request, it is 0. I checked if it was running viewDidLoad()in the test, but it should be because when I just add elements to another array that is outside the request, a specific test is running. I assume this has something to do with the request, and I have not yet found an answer.
Here is the code (this explanation question helped me to execute viewDidLoad () viewController):
ViewController simplified :
class ViewController: UIViewController {
var itemsFromRequest: [Int] = []
var itemsWithoutRequest: [Int] = []
override func viewDidLoad() {
super.viewDidLoad()
itemsWithoutRequest = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Alamofire.request(url: URLConvertible).responseJSON { response in
self.itemsFromRequest = responseData
}
}
}
ViewControllerTests Class:
class ViewControllerTests: XCTestCase {
var viewController: ViewController!
override func setUp() {
super.setUp()
let storyboard = UIStoryboard(name: "Main", bundle: nil)
viewController = storyboard.instantiateViewController(withIdentifier: "ViewController") as! ViewController
let _ = viewController.view
}
func testItemArraysHaveTenItems() {
XCTAssertEqual(viewController.itemsWithoutRequest.count, 10)
XCTAssertEqual(viewController.itemsFromRequest.count, 10)
}
}
Why is the second statement not satisfied?
source
share