The asynchronous nature of RxRuby makes it hard to write good specs for code that returns observables. The complexity of next/error/completed notifications can also easily trip up a naive spec.
The goal of rx-rspec is to provide powerful matchers that lets you express your expectations in a traditional rspec-like synchronous manner.
Currently, the ambition of this project is simply to support the use cases that I encounter in my own usage of RxRuby. Your pull requests are welcome.
rx-rspec is available from http://rubygems.org:
gem install rx-rspec
Once installed, you can use it to assert on your observables:
require 'rx'
require 'rx-rspec'
describe 'awesome' do
subject { Rx::Observable.of(1, 2, 3) }
it { should emit_exactly(1, 2, 3) }
end
You can also set a timeout in seconds so your specs don't block forever in case an observable never emits/completes/errors.
it 'should be fast' do
expect(observable).to emit_exactly(42).within(0.1)
end
rx-spec include the following matchers:
- emit_error(class, message) matches an observable that errors without emitting any values.
- emit_exactly(*events) matches an observable that emits exactly the given values and requires the observable to be completed.
- emit_first(*events) matches against the first values emitted by the observable, but does not require it to complete.
- emit_include(*events) consumes values until the expected values have been seen.
- emit_nothing() matches an observable that completes without emitting values or erroring.