ndsc
ndsc

Reputation: 1213

Unit testing an Observable (Subject<T>)

I got a Repository with a public Subject<Item> called Mutations to which can be subscribed. It's easier to see the observable as a stream of Item of which the source will primary be a socket connection.

I subscribe to the stream using something like Repository.Mutations.Subscribe(item => DoWhatever(item));.

I got a mocked datasource to which I want to push Items to test whether this Stream/Observable in the repository is doing it's job.

I've been reading a lot about using the TestScheduler() in Rx but I'm completely lost when trying to create a unit test and how this scheduler would come into play using my own code.

Admittedly I'm a beginner with Rx but I hope someone would be able to give me a hint or two.

Upvotes: 2

Views: 2480

Answers (1)

lbergnehr
lbergnehr

Reputation: 1598

If what you're after is to see if an item is produced by the Mutations property, you can do like you have, but just verify, in your test code, that the actual subscribe method has been called, i.e. something like:

Item expectedItem = new Item();
Item actualItem = null;
Repository.Mutations.Subscribe(item => actualItem = item);
dataSource.PushItem(expectedItem);

Assert.AreSame(expectedItem, actualItem);

Upvotes: 1

Related Questions