有这个 示例 官方 RX 博客:
There is this example on the official RX blog:
var scheduler = new TestScheduler(); var xs = scheduler.CreateColdObservable( OnNext(10, 42), OnCompleted<int>(20) ); var res = scheduler.Start(() => xs); res.Messages.AssertEqual( OnNext(210, 42), // Subscribed + 10 OnCompleted<int>(220) // Subscribed + 20 ); xs.Subscriptions.AssertEqual( Subscribe(200, 1000) // [Subscribed, Disposed] );我想用reactiveui做这样的事情.我的意思是而不是 scheduler.CreateColdObservable(...) 使用来自实际属性更改通知的流.问题是我尝试了 vm.ObservableForProperty 和 vm.Changed 但它们的工作不一致(并非所有属性更改都创建了一个事件或值为空)
I'd like to do something like this with reactiveui. I mean instead of the scheduler.CreateColdObservable(...) use the streams from actual property change notification. The problem is that I tried vm.ObservableForProperty and vm.Changed but they worked inconsistently (not all property change created an event or the value was null)
这是我的虚拟机的代码:
Here is the code of my VM:
internal class ProductFileEditorVM : ReactiveObject { private readonly List<string> _preloadedList; private bool _OnlyContainingProduct; public bool OnlyContainingProduct { get { return _OnlyContainingProduct; } set { this.RaiseAndSetIfChanged(x => x.OnlyContainingProduct, value); } } private ObservableAsPropertyHelper<IEnumerable<string>> _RepoList; public IEnumerable<string> RepoList { get{return _RepoList.Value;} } public ProductFileEditorVM(RepositoryManager repositoryManager) { //Set defaults OnlyContainingProduct = true; //Preload _preloadedList = repositoryManager.GetList(); var list = this.WhenAny(x => x.OnlyContainingProduct, ocp => ocp.Value ? _preloadedRepoList.Where(repo => repo.ToLower().Contains("product")) : _preloadedRepoList); list.ToProperty(this, x => x.RepoList); } }理想情况下,我想在两个属性上使用 Observable.CombineLatest 并创建一个元组,然后像第一个示例一样在断言表达式中比较这个元组.
Ideally I'd like to use Observable.CombineLatest on the two property and creating a tuple and comparing this tuple in the assert expression like in the first example.
好的结果是:
*或者这是错误的处理方式?我看到的关于此的唯一示例使用了诸如毫秒之类的实际时间度量,但除了 Throttle 和类似方法之外,我看不出它们有什么用处.*
推荐答案所以,因为你不做与时间相关的测试,只做基于顺序的测试(即我做了这个,然后我做了这个,那么它应该是那个),写一个普通的单元测试实际上要简单得多.TestScheduler 是个大锤子:)
So, because you're not doing tests that are related to time, only tests that are based on order (i.e. "I did This, then I did This, then it should be That), it's actually far simpler to just write a normal unit test. TestScheduler is a big hammer :)
因此,您可以执行以下操作:
So, you could do something like:
var fixture = new ProductFileEditorVM(); bool repoListChanged = false; fixture.WhenAny(x => x.RepoList, x => x.Value) .Subscribe(_ => repoListChanged = true); fixture.OnlyContainingProduct = true; Assert.True(repoListChanged);何时使用 TestScheduler
但是,如果加载 RepoList 是异步的并且可能需要一些时间,并且您想要表示正在加载"状态,那么 TestScheduler 会对此有好处 - 您可以在 +20ms、AdvanceTo(200ms) 处单击复选框, 检查您是否处于 Loading 状态,AdvanceTo(10min),然后查看列表是否已更新且状态不是 Loading
When to use TestScheduler
However, if loading RepoList was asynchronous and could take some time, and you wanted to represent a "Loading" state, a TestScheduler would be good for that - you'd click the checkbox at say +20ms, AdvanceTo(200ms), check to see if you're in Loading state, AdvanceTo(10min), then see that the list is updated and the state isn't Loading
更多推荐
ReactiveUI:从单元测试中测试可观察的属性
发布评论