I try to test a function Stream transform(Stream input)
. How can I test if the returned stream emits elements at a certain time?
In RxJS (JavaScript) I can use a TestScheduler to emit elements on the input stream at a certain time and test if they are emitted on the output stream at a certain time. In this example, the transform function is passed to scheduler.startWithCreate
:
var scheduler = new Rx.TestScheduler();
// Create hot observable which will start firing
var xs = scheduler.createHotObservable(
onNext(150, 1),
onNext(210, 2),
onNext(220, 3),
onCompleted(230)
);
// Note we'll start at 200 for subscribe, hence missing the 150 mark
var res = scheduler.startWithCreate(function () {
return xs.map(function (x) { return x * x });
});
// Implement collection assertion
collectionAssert.assertEqual(res.messages, [
onNext(210, 4),
onNext(220, 9),
onCompleted(230)
]);
// Check for subscribe/unsubscribe
collectionAssert.assertEqual(xs.subscriptions, [
subscribe(200, 230)
]);
Aucun commentaire:
Enregistrer un commentaire