如何在此测试中返回可观察值的下一个值?

时间:2019-08-05 14:49:26

标签: swift observable xctest rx-swift

我正在基于以下应用程序进行项目:

MVVMC-SplitViewController

我正在尝试围绕BaseCoordinator类编写单元测试。

我想断言该方法在类中

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

实际上使协调器脱离了childCoordinators字典。

我不确定我该怎么做。我以为我可以简单地创建一个模拟协调器,并让start方法返回某些内容,但是我认为我做错了

我的测试

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)
}

我的基本协调员

   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.")
        }
    }

1 个答案:

答案 0 :(得分:0)

您不能在.empty中使用MockCoordinator作为返回类型。

empty创建了一个Observable,该Observable不发射任何项目,但是可以成功终止。

您还应该更新模拟内容,以便在订阅后也发出值,例如:

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

应该调用该调用以释放您的协调器。