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

Added support to RxTest #35

Merged
merged 4 commits into from
Nov 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## Current Master

- Nothing yet.
- Added support to RxTest. Users may now choose between `RxTest` and `RxBlocking` (or both)

## 4.3.0
- Swift 4.2 support
Expand Down
18 changes: 14 additions & 4 deletions Demo/Demo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
objects = {

/* Begin PBXBuildFile section */
3A4288F2217D7B0000D3651D /* RxNimbleRxTestTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A4288F1217D7B0000D3651D /* RxNimbleRxTestTests.swift */; };
3A4288F4217D7B6200D3651D /* AnyError.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A4288F3217D7B6200D3651D /* AnyError.swift */; };
5E47F32C1C3EAE9B00EC0751 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5E47F32B1C3EAE9B00EC0751 /* AppDelegate.swift */; };
5E47F32E1C3EAE9B00EC0751 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5E47F32D1C3EAE9B00EC0751 /* ViewController.swift */; };
5E47F3311C3EAE9B00EC0751 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 5E47F32F1C3EAE9B00EC0751 /* Main.storyboard */; };
5E47F3331C3EAE9B00EC0751 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 5E47F3321C3EAE9B00EC0751 /* Assets.xcassets */; };
5E47F3361C3EAE9B00EC0751 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 5E47F3341C3EAE9B00EC0751 /* LaunchScreen.storyboard */; };
5E47F3411C3EAE9B00EC0751 /* DemoTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5E47F3401C3EAE9B00EC0751 /* DemoTests.swift */; };
5E47F3411C3EAE9B00EC0751 /* RxNimbleRxBlockingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5E47F3401C3EAE9B00EC0751 /* RxNimbleRxBlockingTests.swift */; };
D04F8C922A4EA6727B35872D /* Pods_Demo.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 9265FBADB809B4B52B402D23 /* Pods_Demo.framework */; };
F189612069FC08D6409FD015 /* Pods_DemoTests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 305E5AFA494DF2A10E0B8767 /* Pods_DemoTests.framework */; };
/* End PBXBuildFile section */
Expand All @@ -30,6 +32,8 @@
/* Begin PBXFileReference section */
305E5AFA494DF2A10E0B8767 /* Pods_DemoTests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_DemoTests.framework; sourceTree = BUILT_PRODUCTS_DIR; };
3388B98AFD3E9302DB045597 /* Pods-DemoTests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-DemoTests.debug.xcconfig"; path = "Pods/Target Support Files/Pods-DemoTests/Pods-DemoTests.debug.xcconfig"; sourceTree = "<group>"; };
3A4288F1217D7B0000D3651D /* RxNimbleRxTestTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RxNimbleRxTestTests.swift; sourceTree = "<group>"; };
3A4288F3217D7B6200D3651D /* AnyError.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AnyError.swift; sourceTree = "<group>"; };
5E47F3281C3EAE9B00EC0751 /* Demo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Demo.app; sourceTree = BUILT_PRODUCTS_DIR; };
5E47F32B1C3EAE9B00EC0751 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
5E47F32D1C3EAE9B00EC0751 /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = "<group>"; };
Expand All @@ -38,7 +42,7 @@
5E47F3351C3EAE9B00EC0751 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
5E47F3371C3EAE9B00EC0751 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
5E47F33C1C3EAE9B00EC0751 /* DemoTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = DemoTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
5E47F3401C3EAE9B00EC0751 /* DemoTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DemoTests.swift; sourceTree = "<group>"; };
5E47F3401C3EAE9B00EC0751 /* RxNimbleRxBlockingTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RxNimbleRxBlockingTests.swift; sourceTree = "<group>"; };
5E47F3421C3EAE9B00EC0751 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
9265FBADB809B4B52B402D23 /* Pods_Demo.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_Demo.framework; sourceTree = BUILT_PRODUCTS_DIR; };
94276AA888ACDCBD57B00C60 /* Pods-Demo.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Demo.release.xcconfig"; path = "Pods/Target Support Files/Pods-Demo/Pods-Demo.release.xcconfig"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -113,7 +117,9 @@
5E47F33F1C3EAE9B00EC0751 /* DemoTests */ = {
isa = PBXGroup;
children = (
5E47F3401C3EAE9B00EC0751 /* DemoTests.swift */,
3A4288F3217D7B6200D3651D /* AnyError.swift */,
5E47F3401C3EAE9B00EC0751 /* RxNimbleRxBlockingTests.swift */,
3A4288F1217D7B0000D3651D /* RxNimbleRxTestTests.swift */,
5E47F3421C3EAE9B00EC0751 /* Info.plist */,
);
path = DemoTests;
Expand Down Expand Up @@ -259,6 +265,7 @@
"${BUILT_PRODUCTS_DIR}/RxBlocking/RxBlocking.framework",
"${BUILT_PRODUCTS_DIR}/RxNimble/RxNimble.framework",
"${BUILT_PRODUCTS_DIR}/RxSwift/RxSwift.framework",
"${BUILT_PRODUCTS_DIR}/RxTest/RxTest.framework",
);
name = "[CP] Embed Pods Frameworks";
outputPaths = (
Expand All @@ -267,6 +274,7 @@
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/RxBlocking.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/RxNimble.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/RxSwift.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/RxTest.framework",
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
Expand Down Expand Up @@ -307,7 +315,9 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
5E47F3411C3EAE9B00EC0751 /* DemoTests.swift in Sources */,
3A4288F4217D7B6200D3651D /* AnyError.swift in Sources */,
3A4288F2217D7B0000D3651D /* RxNimbleRxTestTests.swift in Sources */,
5E47F3411C3EAE9B00EC0751 /* RxNimbleRxBlockingTests.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
5 changes: 5 additions & 0 deletions Demo/DemoTests/AnyError.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import Foundation

enum AnyError: Error {
case any
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,8 @@ import Nimble
import RxSwift
import RxNimble

class RxNimbleTest: QuickSpec {
class RxNimbleRxBlockingTests: QuickSpec {
override func spec() {
/// A type-erased `Swift.Error` for testing purposes
struct AnyError: Swift.Error {
let message: String
init(_ message: String = "") {
self.message = message
}
}

//MARK: First
describe("First") {
it("works with plain observables") {
Expand All @@ -39,7 +31,7 @@ class RxNimbleTest: QuickSpec {

it("get first error") {
let subject = ReplaySubject<Any>.createUnbounded()
subject.onError(AnyError())
subject.onError(AnyError.any)

expect(subject).first.to(throwError())
}
Expand All @@ -64,7 +56,7 @@ class RxNimbleTest: QuickSpec {
it("error, if terminated with error") {
let subject = ReplaySubject<Any>.createUnbounded()
subject.onNext("Hello, world!")
subject.onError(AnyError())
subject.onError(AnyError.any)

expect(subject).last.to(throwError())
}
Expand Down
106 changes: 106 additions & 0 deletions Demo/DemoTests/RxNimbleRxTestTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import Quick
import Nimble
import RxSwift
import RxTest
import RxNimble

class RxNimbleRxTestTests: QuickSpec {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably the worst tests class name ever - absolutely open to suggestions!

override func spec() {
describe("Events") {
let initialClock = 0
var scheduler: TestScheduler!
var disposeBag: DisposeBag!

beforeEach {
disposeBag = DisposeBag()
scheduler = TestScheduler(initialClock: initialClock, simulateProcessingDelay: false)
}

it("works with uncompleted streams") {
let subject = scheduler.createHotObservable([
next(5, "Hello"),
next(10, "World"),
])

expect(subject).events(scheduler: scheduler, disposeBag: disposeBag)
.to(equal([
Recorded.next(5, "Hello"),
Recorded.next(10, "World")
]))
}

it("works with completed streams") {
let subject = scheduler.createHotObservable([
next(5, "Hello"),
next(10, "World"),
completed(100)
])

expect(subject).events(scheduler: scheduler, disposeBag: disposeBag)
.to(equal([
Recorded.next(5, "Hello"),
Recorded.next(10, "World"),
Recorded.completed(100)
]))
}

it("works with errored streams") {
let subject: TestableObservable<String> = scheduler.createHotObservable([
error(5, AnyError.any)
])

expect(subject).events(scheduler: scheduler, disposeBag: disposeBag)
.to(equal([
Recorded.error(5, AnyError.any)
]))
}

it("throws error if any event is error") {
let subject = scheduler.createHotObservable([
Recorded.next(5, "Hello"),
Recorded.next(10, "World"),
error(15, AnyError.any)
])

expect(subject).events(scheduler: scheduler, disposeBag: disposeBag)
.to(throwError())
}

it("does not throw error if no errors") {
let subject = scheduler.createHotObservable([
Recorded.next(5, "Hello"),
Recorded.next(10, "World")
])

expect(subject).events(scheduler: scheduler, disposeBag: disposeBag)
.toNot(throwError())
}

it("subscribes at specified initial time") {
let initialTime = 50
let eventTime = 100
let subject = scheduler.createColdObservable([
next(eventTime, "Hi")
])

expect(subject).events(scheduler: scheduler, disposeBag: disposeBag, startAt: initialTime)
.to(equal([
Recorded.next(initialTime + eventTime, "Hi")
]))
}

it("ignores hot stream events before initial time") {
let subject = scheduler.createHotObservable([
next(5, "Hello"),
next(10, "World"),
completed(100)
])

expect(subject).events(scheduler: scheduler, disposeBag: disposeBag, startAt: 15)
.to(equal([
Recorded.completed(100)
]))
}
}
}
}
2 changes: 1 addition & 1 deletion Demo/Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ target 'DemoTests' do

pod 'Quick'
pod 'Nimble'
pod 'RxNimble', path: '../'
pod 'RxNimble', subspecs: ['RxBlocking', 'RxTest'], path: '../'

end

20 changes: 15 additions & 5 deletions Demo/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,32 @@ PODS:
- Quick (1.3.2)
- RxBlocking (4.3.1):
- RxSwift (~> 4.0)
- RxNimble (4.2.0):
- RxNimble/Core (4.4.0):
- Nimble (~> 7.0)
- RxBlocking (~> 4.0)
- RxSwift (~> 4.2)
- RxNimble/RxBlocking (4.4.0):
- RxBlocking
- RxNimble/Core
- RxNimble/RxTest (4.4.0):
- RxNimble/Core
- RxTest
- RxSwift (4.3.1)
- RxTest (4.3.1):
- RxSwift (~> 4.0)

DEPENDENCIES:
- Nimble
- Quick
- RxNimble (from `../`)
- RxNimble/RxBlocking (from `../`)
- RxNimble/RxTest (from `../`)

SPEC REPOS:
https://github.com/cocoapods/specs.git:
- Nimble
- Quick
- RxBlocking
- RxSwift
- RxTest

EXTERNAL SOURCES:
RxNimble:
Expand All @@ -29,9 +38,10 @@ SPEC CHECKSUMS:
Nimble: 04f732da099ea4d153122aec8c2a88fd0c7219ae
Quick: 2623cb30d7a7f41ca62f684f679586558f483d46
RxBlocking: 64c051285261ca2339481e91b5f70eb06b03660a
RxNimble: cb8c3dd1ca79b83c60744f1c327130c1aef202a1
RxNimble: 170cdfa19fb020c25608760961fd35053f2a2646
RxSwift: fe0fd770a43acdb7d0a53da411c9b892e69bb6e4
RxTest: ea97a208826906f3904c0debdd09575ebcbfe216

PODFILE CHECKSUM: 7405369509db0cbd242146add66181ab5ab0efb0
PODFILE CHECKSUM: 41dbff57d3169ba9e4e1dd63856bcace5b3f96c1

COCOAPODS: 1.5.3
40 changes: 38 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,48 @@ expect(observable).first == 42

Nice.

---

If on the other hand you'd rather use [RxTest](http://cocoapods.org/pods/RxTest) instead of `RxBlocking`, you can do it by specifying RxNimble's `RxTest` subspec. With _RxTest_ you can have more powerful tests, checking a stream as a whole instead of being limited to `first`, `last` and `array` (while the last 2 implicitly require the stream to have completed).

That means _RxTest_ allows you to verify the occurrence of multiple `next`, `error` and `completed` events at specific virtual times:

```
expect(subject).events(scheduler: scheduler, disposeBag: disposeBag)
.to(equal([
Recorded.next(5, "Hello"),
Recorded.next(10, "World"),
Recorded.completed(100)
]))
```

You may also verify specific error types:

```
expect(imageSubject).events(scheduler: scheduler, disposeBag: disposeBag)
.to(equal([
Recorded.error(5, ImageError.invalidImage)
]))
```

## Installation

Add to your podfile:
Add to the tests target in your Podfile:

```rb
pod 'RxNimble' # same as RxNimble/RxBlocking
```

or

```rb
pod 'RxNimble/RxTest' # installs RxTest instead of RxBlocking
```

or even

```rb
pod 'RxNimble'
pod 'RxNimble', subspecs: ['RxBlocking', 'RxTest'] # installs both dependencies
```

And `pod install` and that's it!
Expand Down
25 changes: 20 additions & 5 deletions RxNimble.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "RxNimble"
s.version = "4.3.0"
s.version = "4.4.0"
s.summary = "Nimble extensions that making unit testing with RxSwift easier 🎉"
s.description = <<-DESC
This library includes functions that make testing RxSwift projects easier with Nimble.
Expand All @@ -13,10 +13,25 @@ Pod::Spec.new do |s|
s.osx.deployment_target = "10.10"
s.tvos.deployment_target = "9.0"
s.source = { :git => "https://github.com/RxSwiftCommunity/RxNimble.git", :tag => s.version }
s.source_files = "Source/**/*.swift"
s.dependency "Nimble", "~> 7.0"
s.dependency "RxSwift", "~> 4.2"
s.dependency "RxBlocking", "~> 4.0"
s.default_subspec = "RxBlocking"
Copy link
Contributor Author

@gobetti gobetti Oct 22, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm doing this for compatibility reasons only, but I honestly recommend to use RxTest instead of RxBlocking. We could consider changing the default to RxTest in v5.0, perhaps?!


s.pod_target_xcconfig = { 'ENABLE_BITCODE' => 'NO', 'FRAMEWORK_SEARCH_PATHS' => '$(inherited) "$(PLATFORM_DIR)/Developer/Library/Frameworks"' }

s.subspec "Core" do |ss|
ss.source_files = "Source/Core/"
ss.dependency "Nimble", "~> 7.0"
ss.dependency "RxSwift", "~> 4.2"
end

s.subspec "RxBlocking" do |ss|
ss.source_files = "Source/RxBlocking/"
ss.dependency "RxNimble/Core"
ss.dependency "RxBlocking"
end

s.subspec "RxTest" do |ss|
ss.source_files = "Source/RxTest/"
ss.dependency "RxNimble/Core"
ss.dependency "RxTest"
end
end
File renamed without changes.
File renamed without changes.
17 changes: 17 additions & 0 deletions Source/RxTest/Equal+RxTest.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import Nimble
import RxSwift
@testable import RxTest

/// A Nimble matcher that succeeds when the actual events are equal to the expected events.
public func equal<T: Equatable>(_ expectedEvents: RecordedEvents<T>) -> Predicate<RecordedEvents<T>> {
return Predicate.define { actualEvents in
let actualEquatableEvents = try actualEvents.evaluate()?.map { AnyEquatable(target: $0, comparer: ==) }
let expectedEquatableEvents = expectedEvents.map { AnyEquatable(target: $0, comparer: ==) }

let matches = (actualEquatableEvents == expectedEquatableEvents)
return PredicateResult(bool: matches,
message: .expectedActualValueTo(
"emit <\(stringify(expectedEquatableEvents))>")
)
}
}
Loading