ReactiveCommand在ReactiveUI 6中停止工作

时间:2014-10-12 15:09:20

标签: reactiveui

我正在尝试从Progressive .NET 2014开始使用James Nugent实验室并将其转换为ReactiveUI 6.目前我遇到了CreateAsyncTask,它似乎永远不会运行任务代码。

VM代码包含:

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;

这是失败的测试:

[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);
    });
}

基本上,我遵循了将命令转换为RUI6的指导原则,但似乎没有用。我在任务方法和订阅中设置了一些断点并调试了测试 - 这些断点从未被击中。

1 个答案:

答案 0 :(得分:2)

这是按设计的:

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

将此更改为:

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);