Search code examples
swiftobservablexctestrx-swift

How can I return the next value of an observable in this test?


I am working on a project based on the following app:

MVVMC-SplitViewController

I am trying to write a unit test around the BaseCoordinator class.

I would like to assert that this method within the class

private func free<T: CoordinatorType>(coordinator: T) {
    childCoordinators[coordinator.identifier] = nil
}

does in fact free the coordinator from the childCoordinators dictionary.

I am unsure how I can do this though. I thought I could simply create a mock coordinator and have the start method return something, but I believe I am doing this wrong

My Test

func test_releases_coordinator_from_child_coordinator_dict() {
    class MockCoordinator: BaseCoordinator<Void> {
        override func start() -> Observable<Void> {
            return .empty()
        }
    }

    let mockCoordinator = MockCoordinator()
    let sut = BaseCoordinator<Void>()

    sut.coordinate(to: mockCoordinator).subscribe().disposed(by: disposeBag)

    XCTAssertEqual(sut.childCoordinators.count, 0)
}

My base coordinator

   import RxSwift

    /// Base abstract coordinator generic over the return type of the `start` method.
    class BaseCoordinator<ResultType>: CoordinatorType {

        /// Typealias which allows to access a ResultType of the Coordainator by `CoordinatorName.CoordinationResult`.
        typealias CoordinationResult = ResultType

        /// Utility `DisposeBag` used by the subclasses.
        let disposeBag = DisposeBag()

        /// Unique identifier.
        internal let identifier = UUID()

        /// Dictionary of the child coordinators. Every child coordinator should be added
        /// to that dictionary in order to keep it in memory.
        /// Key is an `identifier` of the child coordinator and value is the coordinator itself.
        /// Value type is `Any` because Swift doesn't allow to store generic types in the array.
        private(set) var childCoordinators = [UUID: Any]()

        /// Stores coordinator to the `childCoordinators` dictionary.
        ///
        /// - Parameter coordinator: Child coordinator to store.
        private func store<T: CoordinatorType>(coordinator: T) {
            childCoordinators[coordinator.identifier] = coordinator
        }

        /// Release coordinator from the `childCoordinators` dictionary.
        ///
        /// - Parameter coordinator: Coordinator to release.
        private func free<T: CoordinatorType>(coordinator: T) {
            childCoordinators[coordinator.identifier] = nil
        }

        /// 1. Stores coordinator in a dictionary of child coordinators.
        /// 2. Calls method `start()` on that coordinator.
        /// 3. On the `onNext:` of returning observable of method `start()` removes coordinator from the dictionary.
        ///
        /// - Parameter coordinator: Coordinator to start.
        /// - Returns: Result of `start()` method.
        func coordinate<T: CoordinatorType, U>(to coordinator: T) -> Observable<U> where U == T.CoordinationResult {
            store(coordinator: coordinator)
            return coordinator.start()
                .do(onNext: { [weak self] _ in self?.free(coordinator: coordinator) })
        }

        /// Starts job of the coordinator.
        ///
        /// - Returns: Result of coordinator job.
        func start() -> Observable<ResultType> {
            fatalError("Start method should be implemented.")
        }
    }

Solution

  • You cannot use .empty as your return type in MockCoordinator.

    empty creates an Observable that emits no items but terminates without fail.

    You should update your mock to emit a value once subscribed too, eg:

    class MockCoordinator: BaseCoordinator<Bool> {
        override func start() -> Observable<Bool> {
            return .of(true)
        }
    }
    

    This should invoke the call to free your coordinator.