There is a public API to facilitate writing unit tests while using RxGo. This is based on rxgo.Assert
. It automatically creates an Observer on an Iterable (Observable, Single or OptionalSingle).
func TestMap(t *testing.T) {
err := errors.New("foo")
observable := rxgo.Just(1, 2, 3)().
Map(func(_ context.Context, i interface{}) (interface{}, error) {
if i == 3 {
return nil, err
}
return i, nil
})
rxgo.Assert(context.Background(), t, observable,
rxgo.HasItems(1, 2),
rxgo.HasError(err))
}
=== RUN TestMap
--- PASS: TestMap (0.00s)
PASS
The API accepts the following list of predicates:
Check whether an Observable produced a given list of items.
rxgo.Assert(ctx, t, observable, rxgo.HasItems(1, 2, 3))
Check whether an Single or OptionalSingle produced a given item.
rxgo.Assert(ctx, t, single, rxgo.HasItem(1))
Check whether an Observable produced a given item regardless of the ordering (useful when we use a pool option)
rxgo.Assert(ctx, t, single, rxgo.HasItemsNoOrder(1, 2, 3))
Check whether an Iterable produced no item(s).
rxgo.Assert(ctx, t, single, rxgo.IsEmpty())
Check whether an Iterable produced any item(s).
rxgo.Assert(ctx, t, single, rxgo.IsNotEmpty())
Check whether an Iterable produced a given error.
rxgo.Assert(ctx, t, single, rxgo.HasError(expectedErr))
Check whether an Iterable produced an error.
rxgo.Assert(ctx, t, single, rxgo.HasAnError())
Check whether an Iterable produced a given list of errors (useful when we use rxgo.ContinueOnError
strategy).
rxgo.Assert(ctx, t, single, rxgo.HasErrors(expectedErr1, expectedErr2, expectedErr3))
Check whether an Iterable did not produced any error.
rxgo.Assert(ctx, t, single, rxgo.HasNoError())
Implement a custom predicate.
rxgo.Assert(ctx, t, observable, rxgo.CustomPredicate(func(items []interface{}) error {
if len(items) != 3 {
return errors.New("wrong number of items")
}
return nil
}))