Trouble unit testing ReactiveCommand CanExecute

691 Views Asked by At

I'm having a world of trouble unit testing a ReactiveCommand. I followed the description describe here ReactiveCommand CanExecute reacting to changes in a collection . The application works fine - buttons are enabled or disabled based on the contents of the list.

My unit tests do not work as expected. I have one as follows :

public void myTest()
{
    var vm = new MyViewModel();
    Assert.IsTrue(vm.Ok.CanExecute(null))

    //Do something that will invalidate the button
    Assert.IsFalse(vm.Ok.CanExecute(null))
}

This test will fail immediately. However, if I subscribe to the CanExecuteObservable, the tests behave as expected :

public void myTest()
{
    var vm = new MyViewModel();

    vm.Ok.CanExecuteObservable.Subscribe(x => {});

    Assert.IsTrue(vm.Ok.CanExecute(null))

    //Do something that will invalidate the button
    Assert.IsFalse(vm.Ok.CanExecute(null))
}

Is this expected behaviour ? I'm using ReactiveUI 6.2.

Also, my unit tests display a lot of "The current thread has no Dispatcher associated with it", in spite of the fact that I have the following in my setup :

[TestInitialize]
public void Init()
{
   //This line throws ...
   RxApp.TaskpoolScheduler = Scheduler.CurrentThread;
}
1

There are 1 best solutions below

0
On

Well I'm an idiot ...

RxApp.MainThreadScheduler = Scheduler.CurrentThread;

Everything starts to look the same after a while ...