Fix: Test Rx methods using RxBlocking

fix #25
This commit is contained in:
Flávio Caetano 2018-03-06 18:53:49 -03:00
parent ce03959fe9
commit 55d8988b77
4 changed files with 82 additions and 145 deletions

View File

@ -12,6 +12,7 @@ target 'ReCaptcha_Example' do
inherit! :search_paths
pod 'AppSwizzle', :git => 'https://github.com/fjcaetano/AppSwizzle.git', :branch => 'swift4'
pod 'RxBlocking', '~> 4.0'
end
target 'ReCaptcha_UITests' do

View File

@ -4,6 +4,8 @@ PODS:
- ReCaptcha/RxSwift (1.1):
- ReCaptcha/Core
- RxSwift (~> 4.0)
- RxBlocking (4.1.1):
- RxSwift (~> 4.0)
- RxCocoa (4.1.1):
- RxSwift (~> 4.0)
- RxSwift (4.1.1)
@ -12,6 +14,7 @@ PODS:
DEPENDENCIES:
- AppSwizzle (from `https://github.com/fjcaetano/AppSwizzle.git`, branch `swift4`)
- ReCaptcha/RxSwift (from `../`)
- RxBlocking (~> 4.0)
- RxCocoa (~> 4.0)
- SwiftLint (~> 0.24)
@ -30,10 +33,11 @@ CHECKOUT OPTIONS:
SPEC CHECKSUMS:
AppSwizzle: bbd3782652fc426ce59c045a92ec61d36f261984
ReCaptcha: b94a673f3827e9b9cf7e77db0395694549241247
RxBlocking: 22e7d7b86a1c0c42164a30d64f56f97128b4ffab
RxCocoa: fd0862fd2df95fa55562ad28ffd2522c25eb4a85
RxSwift: c6e3b1c7b325c7d121cd4327e9d98b7ed746b570
SwiftLint: 2e4b89feed5909c42c3735bbd6745f4345c4b772
PODFILE CHECKSUM: 583c68236539bad71551001f4022b6f2e14d0438
PODFILE CHECKSUM: 3ae211ad7c1b9c86749b013587aa4b63661f345e
COCOAPODS: 1.4.0

View File

@ -431,11 +431,15 @@
);
inputPaths = (
"${SRCROOT}/Pods/Target Support Files/Pods-ReCaptcha_Tests/Pods-ReCaptcha_Tests-frameworks.sh",
"${BUILT_PRODUCTS_DIR}/RxSwift/RxSwift.framework",
"${BUILT_PRODUCTS_DIR}/AppSwizzle/AppSwizzle.framework",
"${BUILT_PRODUCTS_DIR}/RxBlocking/RxBlocking.framework",
);
name = "[CP] Embed Pods Frameworks";
outputPaths = (
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/RxSwift.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/AppSwizzle.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/RxBlocking.framework",
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;

View File

@ -8,6 +8,7 @@
@testable import ReCaptcha
import RxBlocking
import RxCocoa
import RxSwift
import XCTest
@ -15,20 +16,17 @@ import XCTest
class ReCaptcha_Rx__Tests: XCTestCase {
fileprivate var disposeBag: DisposeBag!
fileprivate var apiKey: String!
fileprivate var presenterView: UIView!
override func setUp() {
super.setUp()
disposeBag = DisposeBag()
presenterView = UIApplication.shared.keyWindow!
apiKey = String(arc4random())
}
override func tearDown() {
disposeBag = nil
presenterView = nil
apiKey = nil
@ -42,59 +40,42 @@ class ReCaptcha_Rx__Tests: XCTestCase {
XCTFail("should not ask to configure the webview")
}
do {
// Validate
let result = try manager.rx.validate(on: presenterView)
.toBlocking()
.single()
var result: String?
let exp = expectation(description: "validate token")
// Validate
manager.rx.validate(on: presenterView)
.subscribe { event in
switch event {
case .next(let value):
result = value
case .error(let error):
XCTFail(error.localizedDescription)
case .completed:
exp.fulfill()
}
}
.disposed(by: disposeBag)
waitForExpectations(timeout: 10)
// Verify
XCTAssertEqual(result, apiKey)
// Verify
XCTAssertEqual(result, apiKey)
}
catch let error {
XCTFail(error.localizedDescription)
}
}
func test__Validate__Show_ReCaptcha() {
let manager = ReCaptchaWebViewManager(messageBody: "{action: \"showReCaptcha\"}", apiKey: apiKey)
let exp = expectation(description: "show recaptcha")
var didConfigureWebView = false
manager.configureWebView { _ in
exp.fulfill()
didConfigureWebView = true
}
// Validate
manager.rx.validate(on: presenterView)
.timeout(2, scheduler: MainScheduler.instance)
.subscribe { event in
switch event {
case .next:
XCTFail("should not have validated")
do {
// Validate
_ = try manager.rx.validate(on: presenterView)
.timeout(2, scheduler: MainScheduler.instance)
.toBlocking()
.single()
case .error(let error):
XCTAssertEqual(String(describing: error), RxError.timeout.debugDescription)
case .completed:
XCTFail("should not have completed")
}
}
.disposed(by: disposeBag)
waitForExpectations(timeout: 10)
XCTFail("should have thrown exception")
}
catch let error {
XCTAssertEqual(String(describing: error), RxError.timeout.debugDescription)
XCTAssertTrue(didConfigureWebView)
}
}
@ -104,30 +85,17 @@ class ReCaptcha_Rx__Tests: XCTestCase {
XCTFail("should not ask to configure the webview")
}
var result: ReCaptchaError?
let exp = expectation(description: "validate token")
// Validate
manager.rx.validate(on: presenterView, resetOnError: false)
.subscribe { event in
switch event {
case .next:
XCTFail("should not have validated")
case .error(let error):
result = error as? ReCaptchaError
exp.fulfill()
case .completed:
XCTFail("should not have completed")
}
}
.disposed(by: disposeBag)
waitForExpectations(timeout: 10)
// Verify
XCTAssertEqual(result, .wrongMessageFormat)
do {
// Validate
_ = try manager.rx.validate(on: presenterView, resetOnError: false)
.toBlocking()
.single()
XCTFail("should have thrown exception")
}
catch let error {
XCTAssertEqual(error as? ReCaptchaError, .wrongMessageFormat)
}
}
// MARK: Dispose
@ -142,13 +110,13 @@ class ReCaptcha_Rx__Tests: XCTestCase {
}
let disposable = manager.rx.validate(on: presenterView)
.do(onDispose: exp.fulfill)
.subscribe { _ in
XCTFail("should not validate")
}
disposable.dispose()
DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
exp.fulfill()
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
disposable.dispose()
}
waitForExpectations(timeout: 10)
@ -157,96 +125,56 @@ class ReCaptcha_Rx__Tests: XCTestCase {
// MARK: Reset
func test__Reset() {
let exp1 = expectation(description: "fail on first execution")
let exp2 = expectation(description: "resets after failure")
// Validate
let manager = ReCaptchaWebViewManager(messageBody: "{token: key}", apiKey: apiKey, shouldFail: true)
manager.configureWebView { _ in
XCTFail("should not ask to configure the webview")
}
// Error
let validate = manager.rx.validate(on: presenterView, resetOnError: false)
.share(replay: 1)
do {
// Error
_ = try manager.rx.validate(on: presenterView, resetOnError: false)
.toBlocking()
.single()
}
catch let error {
XCTAssertEqual(error as? ReCaptchaError, .wrongMessageFormat)
validate
.subscribe { event in
switch event {
case .next:
XCTFail("should not have validated")
// Resets after failure
_ = Observable<Void>.just(())
.bind(to: manager.rx.reset)
}
case .error:
exp1.fulfill()
do {
// Resets and tries again
let result = try manager.rx.validate(on: presenterView, resetOnError: false)
.toBlocking()
.single()
case .completed:
XCTFail("should not have completed")
}
}
.disposed(by: disposeBag)
// Resets after failure
validate
.catchErrorJustReturn("error")
.filter { $0 == "error" }
.map { _ in () }
.take(1)
.do(onCompleted: exp2.fulfill)
.bind(to: manager.rx.reset)
.disposed(by: disposeBag)
waitForExpectations(timeout: 10)
// Resets and tries again
let exp3 = expectation(description: "validates after reset")
var result2: String?
manager.rx.validate(on: presenterView, resetOnError: false)
.subscribe { event in
switch event {
case .next(let value):
result2 = value
case .error(let error):
XCTFail(error.localizedDescription)
case .completed:
exp3.fulfill()
}
}
.disposed(by: disposeBag)
waitForExpectations(timeout: 10)
XCTAssertEqual(result2, apiKey)
XCTAssertEqual(result, apiKey)
}
catch let error {
XCTFail(error.localizedDescription)
}
}
func test__Validate__Reset_On_Error() {
let exp = expectation(description: "executes after failure on first execution")
var result: String?
// Validate
let manager = ReCaptchaWebViewManager(messageBody: "{token: key}", apiKey: apiKey, shouldFail: true)
manager.configureWebView { _ in
XCTFail("should not ask to configure the webview")
}
// Error
manager.rx.validate(on: presenterView, resetOnError: true)
.subscribe { event in
switch event {
case .next(let value):
result = value
do {
// Error
let result = try manager.rx.validate(on: presenterView, resetOnError: true)
.toBlocking()
.single()
case .error(let error):
XCTFail(error.localizedDescription)
case .completed:
exp.fulfill()
}
}
.disposed(by: disposeBag)
waitForExpectations(timeout: 10)
XCTAssertEqual(result, apiKey)
XCTAssertEqual(result, apiKey)
}
catch let error {
XCTFail(error.localizedDescription)
}
}
}