So I'm trying to write a unit test to test a view model function however it is an Async function and in the test I'm trying to write it will run synchronously. I've seen a few examples online e.g
listViewModel.$list.sink { _ in
XCTAssertEqual(listViewModel.list?.items.count , 1)
actionExpectation.fulfill()
}
wait(for: [actionExpectation], timeout: 1)
and also trying the solution given here Unit testing an @ObservableObject in Swift Test Driven Development
I've not had any luck trying to test it, any ideas?
ViewModel
class ListViewModel: ObservableObject {
@Published var list: List?
let services: Services
init(services: Services) {
self.services = services
getList()
}
func getList() {
if let listService = self.services.resolve(ListService.self) {
ListService().getList() { [weak self] result in
switch result {
case .success(let dataRecieved):
DispatchQueue.main.async {
self?.list = dataRecieved
}
case .failure(let error):
print(error)
}
}
}
}
}