Recorder
class Recorder<T> {
var items = [T]()
let bag = DisposeBag()
func on(arraySubject: PublishSubject<[T]>) {
arraySubject.subscribe(onNext: { value in
self.items = value
}).disposed(by: bag)
}
func on(valueSubject: PublishSubject<T>) {
valueSubject.subscribe(onNext: { value in
self.items.append(value)
}).disposed(by: bag)
}
}
Then test
final class BookViewModelTests: XCTestCase {
func testBooks() throws {
let expectation = self.expectation(description: #function)
let recorder = Recorder<Book>()
let viewModel = BookViewModel(bookClient: MockBookClient())
recorder.on(arraySubject: viewModel.books)
viewModel.load()
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.5, execute: {
expectation.fulfill()
})
wait(for: [expectation], timeout: 2)
XCTAssertEqual(recorder.items.count, 3)
}
}
Need to use great timeout
value as DispatchQueue
is not guaranteed to be precise, a block needs to wait for the queue to be empty before it can be executed
expectation
less cumbersome
extension XCTestCase {
func waitOrFail(timeout: TimeInterval) {
let expectation = self.expectation(description: #function)
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + timeout, execute: {
expectation.fulfill()
})
wait(for: [expectation], timeout: timeout + 2)
}
}
Original post https://github.com/onmyway133/blog/issues/218
We are a leading niche digital & tech recruitment specialist for the North East of England. We Specialise in the acquisition of high-performing technology talent across a variety of IT sectors including Digital & Technology Software Development.
Our ultimate goal is to make a positive impact on every client and candidate we serve - from the initial call and introduction, right up to the final delivery, we want our clients and candidates to feel they have had a beneficial and productive experience.
If you’re looking to start your journey in sourcing talent or find your dream job, you’ll need a passionate, motivated team of experts to guide you. Check out our Jobs page for open vacancies. If interested, contact us or call 0191 620 0123 for a quick chat with our team.
Follow us on our blog, Facebook, LinkedIn, Twitter or Instagram to follow industry news, events, success stories and new blogs releases.
Back to Blog