views:

210

answers:

1

Hi, I have a ViewModel that starts loading the Model async in the constructor, and triggers an event when the Model is loaded. I got a test working with the silverlight unit test framework, like this :

    bool done = false;

    [TestMethod]
    [Asynchronous]
    public void Test_NoCustomerSelected()
    {
        ProjectListViewModel viewModel = null;
        EnqueueCallback(() => viewModel = new ProjectListViewModel());
        EnqueueCallback(() => viewModel.ModelLoaded += new EventHandler<EventArgs>(viewModel_ModelLoaded));

        EnqueueConditional(() => done);
        EnqueueCallback(() => Assert.IsNotNull(viewModel.FilteredProjectList));
        EnqueueCallback(() => Assert.AreEqual(4, viewModel.FilteredProjectList.Count));

        EnqueueTestComplete();
    }

    void viewModel_ModelLoaded(object sender, EventArgs e)
    {
        done = true;
    }

But I'm beginning playing with Rx Framework, and trying to get my test to work, but so far I have no luck. Here's 2 attempts :

    public void Test_NoCustomerSelected2()
    {
        ProjectListViewModel viewModel = null;
        viewModel = new ProjectListViewModel(eventAggregatorMock.Object, moduleManagerMock.Object);

        IObservable<IEvent<EventArgs>> eventAsObservable = Observable.FromEvent<EventArgs>(
            ev => viewModel.ModelLoaded += ev,
            ev => viewModel.ModelLoaded -= ev);

        eventAsObservable.Subscribe(args => viewModel_ModelLoaded(args.Sender, args.EventArgs));
        eventAsObservable.First();

        Assert.IsNotNull(viewModel.Model);
        Assert.AreEqual(4, viewModel.Model.Count);
    }

    [TestMethod]
    public void Test_NoCustomerSelected3()
    {
        ProjectListViewModel viewModel = null;

        var o = Observable.Start(() => viewModel = new ProjectListViewModel(eventAggregatorMock.Object, moduleManagerMock.Object));

        IObservable<IEvent<EventArgs>> eventAsObservable = Observable.FromEvent<EventArgs>(
            ev => viewModel.ModelLoaded += ev,
            ev => viewModel.ModelLoaded -= ev);

        o.TakeUntil(eventAsObservable)
              .First();

        Assert.IsNotNull(viewModel.Model);
        Assert.AreEqual(4, viewModel.Model.Count);
    }

The first test goes in waiting forever, the second doesn't work because the viewModel is null when it does the FromEvent.

Anyone has a clue on how to do this properly?

UPDATE: Here's a working solution with the help from PL :

[TestMethod]
public void Test_NoCustomerSelected4()
{
    var viewModel = new ProjectListViewModel(eventAggregatorMock.Object, moduleManagerMock.Object); 

Observable 
   .FromEvent<EventArgs>(viewModel, "ModelLoaded")) 
   .Take(1) 
   .Subscribe(e =>  
       { 
           Assert.IsNotNull(viewModel.Model); 
           Assert.AreEqual(4, viewModel.Model.Count); 
       }); 
}
+1  A: 

I believe you still have to use Enqueue calls. E.g. something like this:

var done = false;
var viewModel = new ProjectListViewModel(eventAggregatorMock.Object, moduleManagerMock.Object);

Observable
   .FromEvent<EventArgs>(viewModel, "ModelLoaded"))
   .Take(1)
   .Subscribe(viewModel => 
       {
           Assert.IsNotNull(viewModel.Model);
           Assert.AreEqual(4, viewModel.Model.Count);
           done = true;
       });

EnqueueConditional(() => done);
EnqueueTestComplete();
PL
This works, only problems where that I had to change to .Subscribe(e =>...) and that the done en Enqueue are not needed. Thanks PL.
Christian Lavallee