title: Basic RX TestScheduler timing
body: <p>I'm using RX to do some (pretty basic) event subscription: -</p>
  
  <pre><code>public void StartListening(IObservable&lt;Item&gt; observable)
  {
     subscription = observable
        .Buffer(TimeSpan.FromSeconds(5))
        .Where(Enumerable.Any)
        .Subscribe(events =&gt; service.DoStuffInBatch(events));
  }
  </code></pre>
  
  <p>Now all I want is a set of unit tests that prove the behaviour of this method. Things like: -</p>
  
  <ul>
  <li>If I get data but time has not yet gone 5 seconds, we never call DoStuffInBatch.</li>
  <li>If I get data and time has passed 5 seconds, I call DoStuffInBatch with the data.</li>
  <li>If I don't get data and time has passed 5 seconds, I don't call DoStuffInBatch.</li>
  <li>If I get data over two windows e.g. some at 3 seconds and some at 8 seconds, I make two calls to DoStuffInBatch.</li>
  </ul>
  
  <p>I have been reading through all the examples that Bart has put out (e.g. <a href="http://blogs.msdn.com/b/rxteam/archive/2012/06/14/testing-rx-queries-using-virtual-time-scheduling.aspx" rel="nofollow">http://blogs.msdn.com/b/rxteam/archive/2012/06/14/testing-rx-queries-using-virtual-time-scheduling.aspx</a>) but I'm really struggling with my tests. Here's an example: -</p>
  
  <pre><code>[Test]
  public void StartListening_SomeDataAndBatchTimeHasNotPassed_DoesNotDoStuffInBatch()
  {
      var scheduler = new TestScheduler();
  
      // Act - call the class under test, supplying a set of dummy data on the test scheduler
      listener.StartListening(GenerateTestData(5).ToObservable(scheduler));
  
      // Move the scheduler forward by one second
      scheduler.AdvanceBy(TimeSpan.FromSeconds(1).Ticks);
  
      // Assert that we never called the service as we haven't yet advanced 5 seconds
      Mock.Assert(() =&gt; mockService.DoStuffInBatch(null), Args.Ignore(), Occurs.Never());
  }
  </code></pre>
  
  <p>However, this test fails. As soon as I call AdvanceBy, the subscription fires and calls DoStuffInBatch.</p>
  
  <p>I'm clearly doing something wrong - but haven't got a clue what I'm doing wrong.</p>