Skip to content

Examples

Ruby Lichtenshtein edited this page Jan 2, 2018 · 9 revisions
  • complete()

Observable.just("Hello RxTest!")
    .test {
        it should complete()
    }
  • notComplete()

PublishSubject.create<String>()
    .test {
       it should notComplete()
     }
  • error(error: Throwable)

val assertionError = AssertionError()
PublishSubject.create<String>()
        .apply {
            onError(assertionError)
        }
        .test {
            it shouldHave error(assertionError)
        }
  • error(errorClass: Class)

PublishSubject.create<String>()
        .apply {
            onError(AssertionError())
        }
        .test {
            it shouldHave error(AssertionError::class.java)
        }
  • error(errorPredicate: (Throwable) -> Boolean)

val assertionError = AssertionError()
PublishSubject.create<String>()
        .apply {
            onError(assertionError)
        }
        .test {
            it shouldHave error(Predicate { it == assertionError })
        } 
  • noErrors()

PublishSubject.create<String>()
        .test {
            it shouldHave noErrors()
        } 
  • value(t: T)

val value = "a"
val obs = Observable.just(value)
obs.test {
    it shouldHave value(value)
}

obs.test {
    it shouldEmit value
}
  • value(predicate: (T) -> Boolean)`

val value = "a"
val obs = Observable.just(value)

obs.test {
    it shouldHave value(Predicate { it == value })
}

obs.test {
    it shouldEmit Predicate { it == value }
}
  • values(vararg values: T)

val obs = Observable.just("Hello", "RxTest")

obs.test {
    it shouldEmit values("Hello", "RxTest")
}

obs.test {
    it shouldEmit values("Hello", "RxTest")
}
  • valueSequence(sequence: Iterable)

val valueSequence = listOf("Hello", "RxTest")
val obs = Observable.fromIterable(valueSequence)

obs.test {
    it shouldEmit valueSequence(valueSequence)
}
  • valueSet(set: Collection)

val valueSequence = listOf("Hello", "RxTest")
val obs = Observable.fromIterable(valueSequence)

obs.test {
    it shouldEmit valueSet(valueSequence)
}
  • valueOnly(vararg values: T)

ReplaySubject.create<String>()
        .also {
            it.onNext("Hello")
            it.onNext("Kotlin")
        }
        .test {
            it shouldEmit valueOnly("Hello", "Kotlin")
        }
  • valueCount(count: Int)

Observable.just("Bloop")
        .test {
            it shouldHave valueCount(1)
        }
  • never(t: T)

val value = "a"
val never = "b"
val obs = Observable.just(value)
val valuePredicate = Predicate { v: String -> v == never }

    obs.test {
        it should never(never)
    }

    obs.test {
        it shouldNeverEmit never
    }
  • never(predicate: (T) -> Boolean)

val value = "a"
val never = "b"
val obs = Observable.just(value)
val valuePredicate = Predicate { v: String -> v == never }

    obs.test {
        it should never(valuePredicate)
    }

    obs.test {
        it shouldNeverEmit valuePredicate
    }
  • valueAt(index: Int, value: T)

val value0 = "a"
val value1 = "b"
val obs = Observable.just(value0, value1)

obs.test {
    it shouldHave valueAt(0, value0)
    it shouldHave valueAt(1, value1)
}
  • valueAt(index: Int, valuePredicate: (T) -> Boolean)

val value0 = "a"
val value1 = "b"
val obs = Observable.just(value0, value1)

obs.test {
    it shouldHave valueAt(0, Predicate { value -> value == value0 })
    it shouldHave valueAt(1, Predicate { value -> value == value1 })
}
  • empty()

PublishSubject.create<String>()
        .test {
            it shouldBe empty()
        }
  • timeout()

val scheduler = TestScheduler()
Observable
        .interval(100, TimeUnit.MILLISECONDS, scheduler)
        .test {
            it.await(50, TimeUnit.MILLISECONDS)
            it shouldHave timeout()
        }
  • noTimeout()

val scheduler = TestScheduler()
Observable
        .interval(100, TimeUnit.MILLISECONDS, scheduler)
        .test {
            it shouldHave noTimeout()
        }
  • subscribed()

Observable.just("Wabba Labba!")
        .apply {
            subscribe({ })
            test {
                it should subscribed()
            }
        }
  • notSubscribed()

TestObserver<String>()
        .apply { 
            should(notSubscribed())
        }
  • failure(errorPredicate: (Throwable) -> Boolean, vararg values: T)

val value0 = "a"
val value1 = "b"
val errorMessage = "Error"
val error = Throwable(errorMessage)
val obs = ReplaySubject.create<String>()
        .also {
            it.onNext(value0)
            it.onNext(value1)
            it.onError(error)
        }

obs.test {
    it shouldHave failure(Predicate { true }, value0, value1)
}
  • failure(error: Class, vararg values: T)

val value0 = "a"
val value1 = "b"
val errorMessage = "Error"
val error = Throwable(errorMessage)
val obs = ReplaySubject.create<String>()
        .also {
            it.onNext(value0)
            it.onNext(value1)
            it.onError(error)
        }

obs.test {
    it shouldHave failure(Throwable::class.java, value0, value1)
}
  • failureAndMessage(error: Class, message: String, vararg values: T)

val value0 = "a"
val value1 = "b"
val errorMessage = "Error"
val error = Throwable(errorMessage)
val obs = ReplaySubject.create<String>()
        .also {
            it.onNext(value0)
            it.onNext(value1)
            it.onError(error)
        }

obs.test {
    it shouldHave failureAndMessage(Throwable::class.java, errorMessage, value0, value1)
}
  • result(vararg values: T)

val value0 = "a"
val value1 = "b"
val values = listOf(value0, value1)
Observable.just(values)
        .test {
            it shouldHave result(values)
        }
  • terminate()

Observable.just(item0)
        .test {
            it should terminate()
        }
Observable.error<String>(Throwable())
        .test {
            it should terminate()
        }