Skip to content

Tiny extension of XCTWaiter, which helps to delay execution of XCTAssert in synchronous way, without fulfilling expectation.

Notifications You must be signed in to change notification settings

pavliuko/XCTWaiterTimedOutExtension

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 

Repository files navigation

Source

extension XCTWaiter {
    static func waitForExpectation(withDescription: String,
                                   timedOut seconds: TimeInterval,
                                   andVerify assertsBlock:(() -> Void)) {
        let expectation = XCTestExpectation(description: withDescription)
        let _ = XCTWaiter.wait(for: [expectation], timeout: seconds)
        assertsBlock()
    }
}

Usage

final class SomeMediatorTests: XCTestCase {

    var api: APIServiceMock!
    var view: ViewControllerMock!
    var mediator: SomeMediatorProtocol!

    override func setUp() {
        super.setUp()
        api = APIServiceMock()
        view = ViewControllerMock()
        mediator = SomeMediator(api: api, view: view)
    }

    func testRefreshUnreadNotificationsEndedSuccessfully() {
        api.backendModel = BackendNotificationsCodable.default

        mediator.refreshUnreadNotifications()

	// Closure will be executed after delay, in that case all async 
	// operations inside view updating process will have time to execute
        XCTWaiter.waitForExpectation(
            withDescription: "Waiting for notifications refresh",
            timedOut: 1,
            andVerify: {
            	// In the end of process, mediator should trigger view to
		// update and give new viewModel for that, error should be nil
                XCTAssertNotNil(self.view.viewModel)
                XCTAssertNil(self.view.error)
        }
        )
    }

    func testRefreshUnreadNotificationsEndedFailure() {
        api.error = SomeError.incorrectRequestError

        mediator.refreshUnreadNotifications()

        XCTWaiter.waitForExpectation(
            withDescription: "Waiting for notifications refresh failed",
            timedOut: 1,
            andVerify: {
            	// In the end of process, mediator should trigger view to 
		// update and give error instance, viewModel should be nil
                XCTAssertNil(self.view.viewModel)
                XCTAssertNotNil(self.view.error)
        }
        )
    }
}

About

Tiny extension of XCTWaiter, which helps to delay execution of XCTAssert in synchronous way, without fulfilling expectation.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages