swift - 如何在此测试中返回 observable 的下一个值?
问题描述
我正在开发一个基于以下应用程序的项目:
我正在尝试围绕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.")
}
}
解决方案
您不能.empty
在MockCoordinator
.
empty
创建一个不发射任何项目但不会失败地终止的 Observable。
您应该更新您的模拟以在订阅后发出一个值,例如:
class MockCoordinator: BaseCoordinator<Bool> {
override func start() -> Observable<Bool> {
return .of(true)
}
}
这应该调用释放您的协调器的调用。
推荐阅读
- html - 将滚动条定位在元素内
- c# - IDistributedCache 通过注入调用构造函数
- python - 在两个 python 列表中查找常见项目的索引
- android - 如何在xml中的Ternaroy运算符中使用三元条件,同时将数据设置为具有android数据绑定的TextView
- c# - 将 Json 格式的消息从 ac# 应用程序发送到服务器
- javascript - 验证 HTML 输入字段并使用 JavaScript 重定向到 Web 链接
- html - 在 angularjs 中添加一个 html,包括 ng-click
- c - C中的任何数字除以0,它是否属于分段错误?
- python - 合并每组以填充时间序列
- java - 如何使用 Selenium webdriver 和 Java 读取 PDF