首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何返回在此测试中可观察到的下一个值?

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

Stack Overflow用户
提问于 2019-08-05 14:49:26
回答 1查看 169关注 0票数 0

我正在做一个基于以下应用程序的项目:

MVVMC-SplitViewController

我试图围绕BaseCoordinator类编写一个单元测试。

我想断言类中的这个方法

代码语言:javascript
复制
private func free<T: CoordinatorType>(coordinator: T) {
    childCoordinators[coordinator.identifier] = nil
}

实际上将协调器从childCoordinators字典中释放出来。

我不知道我怎么能做到这一点。我以为我可以简单地创建一个模拟协调器,让start方法返回一些东西,但我认为我做错了

我的测试

代码语言:javascript
复制
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)
}

我的基地协调员

代码语言:javascript
复制
   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.")
        }
    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-08-05 14:56:15

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

empty创建了一个可观察的程序,它不会发出任何项目,但不会失败。

您应该更新模拟以发出订阅后的值,例如:

代码语言:javascript
复制
class MockCoordinator: BaseCoordinator<Bool> {
    override func start() -> Observable<Bool> {
        return .of(true)
    }
}

这应该会调用释放协调程序。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57361031

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档