Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Testability of Reactors #5

Closed
robinmayerhofer opened this issue Jul 27, 2020 · 0 comments · Fixed by #6
Closed

Testability of Reactors #5

robinmayerhofer opened this issue Jul 27, 2020 · 0 comments · Fixed by #6
Labels
enhancement New feature or request

Comments

@robinmayerhofer
Copy link

Reactors are not easily testable, because they do not use the same queue as the tests (see Reactor.swift)

In SwiftUIReactor, simple reactors cannot be tested synchronously (test fails):

import XCTest
import Combine
@testable import SwiftUIReactor

final class TestConcurrency: XCTestCase {
    func testCountingUp() {

        for exponent in 0..<8 {
            let reactor = CountingReactor()

            let _amount: Double = pow(10, Double(exponent))
            let amount = Int(_amount)

            print("Testing", amount)

            for _ in 0..<amount {
                reactor.action(.countUp)
            }

            XCTAssertEqual(reactor.state.currentCount, amount)
        }
    }
}

final class CountingReactor: Reactor {

    enum Action {
        case countUp
    }

    enum Mutation {
        case countUp
    }

    struct State {
        var currentCount: Int
    }

    var cancellables: Set<AnyCancellable> = []

    var state: State = State(currentCount: 0)

    func mutate(action: Action) -> AnyPublisher<Mutation, Never> {
        return Just(Mutation.countUp).eraseToAnyPublisher()
    }

    func reduce(mutation: Mutation) {
        var newState = state

        newState.currentCount += 1

        state = newState
    }

}

In ReactorKit this is circumvented via the CurrentThreadScheduler.instance, see here.
Simple reactors can, therefore, be tested synchronously (test succeeds):

import XCTest
import RxSwift
@testable import ReactorKit

final class TestConcurrency: XCTestCase {
    func testCountingUp() {
        for exponent in 0...5 {
            let reactor = CountingReactor()

            let _amount: Double = pow(10, Double(exponent))
            let amount = Int(_amount)

            print("Testing", amount)

            for _ in 0..<amount {
                reactor.action.onNext(.countUp)
            }

            XCTAssertEqual(reactor.currentState.currentCount, amount)
        }


    }
}

final class CountingReactor: Reactor {

    enum Action {
        case countUp
    }

    struct State {
        var currentCount: Int
    }

    let initialState = State(currentCount: 0)

    func mutate(action: Action) -> Observable<Action> {
        return .just(Mutation.countUp)
    }

    func reduce(state: State, mutation: Action) -> State {
        var newState = state

        newState.currentCount += 1

        return newState
    }

}
@julianpomper julianpomper added the enhancement New feature or request label Jul 27, 2020
@julianpomper julianpomper mentioned this issue Aug 1, 2020
@julianpomper julianpomper linked a pull request Aug 1, 2020 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants