Alexey Zimarev
Alexey Zimarev

Reputation: 19610

ReactiveCommand stopped working in ReactiveUI 6

I'm trying to work on James Nugent labs from the Progressive .NET 2014 and convert it to ReactiveUI 6. Currently I got stuck with CreateAsyncTask, it seems to never run the task code.

The VM code has this:

private IObservable<string> PretendToCallTheServer()
{
    return Observable.Return("Hello World")
        .Delay(TimeSpan.FromMilliseconds(2000), RxApp.TaskpoolScheduler);
} 

public PersonViewModel()
{
    _fullName = this.WhenAnyValue(vm => vm.FirstName, vm => vm.LastName, (f, l) => string.Format("{0} {1}", f, l))
                    .ToProperty(this, vm => vm.FullName);

    var firstAndLastFilled = this.WhenAnyValue(vm => vm.FirstName, vm => vm.LastName,
        (f, l) => !string.IsNullOrWhiteSpace(f) && !string.IsNullOrWhiteSpace(l));

    ChangeName = ReactiveCommand.CreateAsyncTask(firstAndLastFilled, async x => await PretendToCallTheServer());
    ChangeName.Subscribe(x => ServerResult = x);
}

private string _serverResult;
public string ServerResult
{
    get { return _serverResult; }
    set { this.RaiseAndSetIfChanged(ref _serverResult, value); }
}

public readonly ReactiveCommand<string> ChangeName;

And here is the failing test:

[Test]
public void ChangeNamePretendsToCallServerButOurTestRunsFast()
{
    (new TestScheduler()).With(sched =>
    {
        var sut = new PersonViewModel {FirstName = "Jane", LastName = "Appleseed"};
        Assert.IsTrue(sut.ChangeName.CanExecute(null));

        sut.ChangeName.ExecuteAsync();

        sched.AdvanceByMs(1000);
        Assert.AreEqual(null, sut.ServerResult);

        sched.AdvanceByMs(10000);
        Assert.AreEqual("Hello World", sut.ServerResult);
    });
}

Basically, I followed the guidelines on conversion of commands to RUI6 but it seems not to work. I set some breakpoints in the task method and on subscription and debugged the test - these breakpoints were never hit.

Upvotes: 2

Views: 820

Answers (1)

Ana Betts
Ana Betts

Reputation: 74654

This is by-design:

// You didn't Subscribe, so it doesn't do anything
sut.ChangeName.ExecuteAsync();

Change this to something like:

var result = default(string);
sut.ChangeName.ExecuteAsync().Subscribe(x => result = x);

sched.AdvanceByMs(1000);
Assert.AreEqual(null, result);

sched.AdvanceByMs(10000);
Assert.AreEqual("Hello World", result);

Upvotes: 3

Related Questions