在我的测试用例中,我可以验证事件处理程序CollectionChanged
是否正确连接。创建命令时会发生这种情况。在我的视图模型中,当我做同样的事情时,事件处理程序永远不会被连接起来。这是为什么?
如果我在视图模型中对Undo.CanExecute(null)
进行显式调用,它将挂钩事件处理程序。我想我不应该这样做,而且我的视图模型代码肯定有问题。
ActionManager = new ActionManager();
var canUndo = Observable
.FromEventPattern(e => ActionManager.CollectionChanged += e, e => ActionManager.CollectionChanged -= e)
.Select(_ => ActionManager.CanUndo)
;
Undo = ReactiveCommand.CreateAsyncTask(canUndo, UndoAsync);
public class MiscTests
{
[Fact]
public void CanExecute()
{
var am = new ActionManager();
var canUndo = Observable
.FromEventPattern(e => am.CollectionChanged += e, e => am.CollectionChanged -= e)
.Select(_ => am.CanUndo);
var command = ReactiveCommand.Create(canUndo);
var action = new CallMethodAction(() => { }, () => { });
var canExecute = command.CanExecute(null);
canExecute.Should().BeFalse();
am.Execute(action);
canExecute = command.CanExecute(null);
canExecute.Should().BeTrue();
}
}