Note that there are some explanatory texts on larger screens.

plurals
  1. POUnit testing for an event using Reactive Extensions
    primarykey
    data
    text
    <p>I'm using <a href="http://msdn.microsoft.com/en-us/devlabs/ee794896.aspx" rel="noreferrer">Reactive Extensions for .NET (Rx)</a> to expose events as <code>IObservable&lt;T&gt;</code>. I want to create an unit test where I assert that a particular event is fired. Here is a simplified version of the class I want to test:</p> <pre><code>public sealed class ClassUnderTest : IDisposable { Subject&lt;Unit&gt; subject = new Subject&lt;Unit&gt;(); public IObservable&lt;Unit&gt; SomethingHappened { get { return this.subject.AsObservable(); } } public void DoSomething() { this.subject.OnNext(new Unit()); } public void Dispose() { this.subject.OnCompleted(); } } </code></pre> <p>Obviously my real classes are more complex. My goal is to verify that performing some actions with the class under test leads to a sequence of events signaled on the <code>IObservable</code>. Luckily the classes I want to test implement <code>IDisposable</code> and calling <code>OnCompleted</code> on the subject when the object is disposed makes it much easier to test.</p> <p>Here is how I test:</p> <pre><code>// Arrange var classUnderTest = new ClassUnderTest(); var eventFired = false; classUnderTest.SomethingHappened.Subscribe(_ =&gt; eventFired = true); // Act classUnderTest.DoSomething(); // Assert Assert.IsTrue(eventFired); </code></pre> <p>Using a variable to determine if an event is fired isn't too bad, but in more complex scenarios I may want to verify that a particular sequence of events are fired. Is that possible without simply recording the events in variables and then doing assertions on the variables? Being able to use a fluent LINQ-like syntax to make assertions on an <code>IObservable</code> would hopefully make the test more readable.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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