Rx.TestScheduler

Virtual time scheduler used for testing applications and libraries built using Reactive Extensions. This inherits from the Rx.TestScheduler class.

Usage ##

The following shows an example of using the Rx.TestScheduler. In order to make the end comparisons work, you must implement a collection assert, for example here using QUnit.

function createMessage(actual, expected) {
    return 'Expected: [' + expected.toString() + ']\r\nActual: [' + actual.toString() + ']';
}

// Using QUnit testing for assertions
var collectionAssert = {
    assertEqual: function (expected, actual) {
        var comparer = Rx.Internals.isEqual,
            isOk = true;

        if (expected.length !== actual.length) {
            ok(false, 'Not equal length. Expected: ' + expected.length + ' Actual: ' + actual.length);
            return;
        }

        for(var i = 0, len = expected.length; i < len; i++) {
            isOk = comparer(expected[i], actual[i]);
            if (!isOk) {
                break;
            }
        }

        ok(isOk, createMessage(expected, actual));
    }
};

var onNext = Rx.ReactiveTest.onNext,
    onCompleted = Rx.ReactiveTest.onCompleted,
    subscribe = Rx.ReactiveTest.subscribe;

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)
]);

Location

  • rx.testing.js

TestScheduler Constructor ##

TestScheduler Instance Methods ##

Inherited Classes ##

TestScheduler Constructor ##

Rx.TestScheduler()

#

Creates a new virtual time test scheduler.

Example

Location

  • rx.testing.js

TestScheduler Instance Methods ##

Rx.TestScheduler.prototype.createColdObservable(...args)

#

Creates a cold observable using the specified timestamped notification messages.

Arguments

  1. args (Arguments): An arguments array of Recorded objects from Rx.ReactiveTest.onNext, Rx.ReactiveTest.onError, and Rx.ReactiveTest.onCompleted methods.

Returns

(Observable): Cold observable sequence that can be used to assert the timing of subscriptions and notifications.

Example

Location

  • rx.testing.js

Rx.TestScheduler.prototype.createHotObservable(...args)

#

Creates a hot observable using the specified timestamped notification messages.

Arguments

  1. args (Arguments): An arguments array of Recorded objects from Rx.ReactiveTest.onNext, Rx.ReactiveTest.onError, and Rx.ReactiveTest.onCompleted methods.

Returns

(Observable): Hot observable sequence that can be used to assert the timing of subscriptions and notifications.

Example

Location

  • rx.testing.js

Rx.TestScheduler.prototype.createObserver()

#

Creates an observer that records received notification messages and timestamps those.

Returns

(Observer): Observer that can be used to assert the timing of received notifications.

Example

Location

  • rx.testing.js

Rx.TestScheduler.prototype.startWithCreate(create)

#

Starts the test scheduler and uses default virtual times to invoke the factory function, to subscribe to the resulting sequence, and to dispose the subscription.

Arguments

  1. create (Function): Factory method to create an observable sequence.

Returns

(Observer): Observer with timestamped recordings of notification messages that were received during the virtual time window when the subscription to the source sequence was active.

Example

Location

  • rx.testing.js

Rx.TestScheduler.prototype.startWithDispose(create, disposed)

#

Starts the test scheduler and uses the specified virtual time to dispose the subscription to the sequence obtained through the factory function. Default virtual times are used for factory invocation and sequence subscription.

Arguments

  1. create (Function): Factory method to create an observable sequence.

  2. disposed (Number): Virtual time at which to dispose the subscription.

Returns

(Observer): Observer with timestamped recordings of notification messages that were received during the virtual time window when the subscription to the source sequence was active.

Example

Location

  • rx.testing.js

Rx.TestScheduler.prototype.startWithTiming(create, created, subscribed, disposed)

#

Starts the test scheduler and uses the specified virtual times to invoke the factory function, subscribe to the resulting sequence, and dispose the subscription.

Arguments

  1. create (Function): Factory method to create an observable sequence.

  2. created (Number): Virtual time at which to invoke the factory to create an observable sequence.

  3. subscribed (Number): Virtual time at which to subscribe to the created observable sequence.

  4. disposed (Number): Virtual time at which to dispose the subscription.

Returns

(Observer): Observer with timestamped recordings of notification messages that were received during the virtual time window when the subscription to the source sequence was active.

Example

Location

  • rx.testing.js

Last updated

Was this helpful?