gpt4 book ai didi

c# - TestScheduler 在订阅的属性上没有像预期的那样工作(w throttle)

转载 作者:行者123 更新时间:2023-11-30 20:45:58 27 4
gpt4 key购买 nike

我对 rx/ReactiveUi 非常陌生,想使用 TestScheduler 编写一个 xunit 测试来检查检索搜索建议的限制是否正常工作。

想法是使用 TestScheudler 进行计时,更改 search-term 属性的值并检查是否调用了异步方法。不幸的是,该方法没有在预期的位置调用(见附件代码,尤其是单元测试)。

我错过了什么?我尝试测试的方法是否是一个好方法?

我的 View 模型:

public class MyViewModel : ReactiveObject
{
public MyViewModel (IMyQueryHandler queryHandler)
{
...
// Type suggestions
this.SearchTerms = this.ObservableForProperty(x => x.SearchTerm)
.Throttle(SuggestionThrottle).Value();

this.SearchTerms.Subscribe(this.LoadSearchSuggestionsAsync);
...
}

internal async void LoadSearchSuggestionsAsync(string search)
{
...
this.SearchSuggestions = this.queryHandler.ExecuteQuery(...);
...
}

public IList<SearchSuggestion> SearchSuggestions
{
get { return this.searchSuggestions; }
set { this.RaiseAndSetIfChanged(ref this.searchSuggestions, value); }
}

...
}

我的单元测试 (xunit):

...

public class TestFixture : ReactiveObject
{
public string SearchTerms { get { return this._searchTermsBackingField.Value; } }
public ObservableAsPropertyHelper<string> _searchTermsBackingField;
}

[Fact]
public void WillTryToLoadSearchSuggestionsAfterThrottleTime()
{
new TestScheduler().With(
sched =>
{
var fixture = new TestFixture();
var queryClient = Substitute.For<IMyQueryHandler>();

var caseSuggestions = new List<...> { ... }

queryClient.ExecuteQuery<...>(...).ReturnsForAnyArgs(...); // Nsubstitute

var vm = new MyViewModel(queryClient);

vm.SearchTerms.ToProperty(fixture, p => p.SearchTerms, out fixture._searchTermsBackingField);

sched.Schedule(() => vm.SearchTerm = "Tes");
sched.Schedule(MyViewModel.SuggestionThrottle, () => vm.SearchTerm = "Test");

sched.AdvanceBy(MyViewModel.SuggestionThrottle.Ticks);
sched.AdvanceBy(1);

// why is the method MyViewModel.LoadSearchSuggestionsAsync not called here (in debug)???

sched.AdvanceBy(1);
} // method gets called here...
}
...

最佳答案

Throttle 没有设置调度器,而是写 Throttle(timespan, RxApp.MainThreadScheduler)

关于c# - TestScheduler 在订阅的属性上没有像预期的那样工作(w throttle),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27549753/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com