Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This kind of testing for observables would be incomplete. Just recently, RX team published the test scheduler and some extensions (which BTW they use internally for testing the library). Using these, you can not only check if something happened or not, but also make sure the <strong>timing and order</strong> is correct. As a bonus the test scheduler allows you to run your tests in "virtual time", so the tests run instantly, no matter how big delays you're using inside.</p> <p>Jeffrey van Gogh from RX team <a href="http://blogs.msdn.com/b/jeffva/archive/2010/08/27/testing-rx.aspx" rel="nofollow">published an article on how to do such kind of testing.</a> </p> <p>The above test, using the mentioned approach, will look like this:</p> <pre><code> [TestMethod] public void SimpleTest() { var sched = new TestScheduler(); var subject = new Subject&lt;Unit&gt;(); var observable = subject.AsObservable(); var o = sched.CreateHotObservable( OnNext(210, new Unit()) ,OnCompleted&lt;Unit&gt;(250) ); var results = sched.Run(() =&gt; { o.Subscribe(subject); return observable; }); results.AssertEqual( OnNext(210, new Unit()) ,OnCompleted&lt;Unit&gt;(250) ); }: </code></pre> <p>EDIT: You can also call .OnNext (or some other method) implicitly:</p> <pre><code> var o = sched.CreateHotObservable(OnNext(210, new Unit())); var results = sched.Run(() =&gt; { o.Subscribe(_ =&gt; subject.OnNext(new Unit())); return observable; }); results.AssertEqual(OnNext(210, new Unit())); </code></pre> <p>My point is - in simplest situations, you need just to make sure the event is fired (f.e. you are checking your Where is working correctly). But then as you progress in complexity, you start testing for timing, or completion, or something else that requires the virtual scheduler. But the nature of the test using virtual scheduler, opposite to "normal" tests is to test the entire observable at once, not "atomic" operations. </p> <p>So probably you would have to switch to the virtual scheduler somewhere down the road - why not start with it in the beginning?</p> <p>P.S. Also, you would have to resort to a different logic for each test case - f.e. you would have very different observable for testing that something did not happen opposite to that something happened.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload