C# 单元测试反应式UI视图模型和命令
我有一个使用ReactiveUI实现的工作视图,现在我正试图为我的视图模型编写一些单元测试,但是当从测试中使用时,我的视图模型似乎无法工作 具体来说,执行命令似乎不会触发订阅服务器。在下面的测试中,我正在调用AddPlayer命令,但订阅的处理程序未运行:C# 单元测试反应式UI视图模型和命令,c#,reactiveui,C#,Reactiveui,我有一个使用ReactiveUI实现的工作视图,现在我正试图为我的视图模型编写一些单元测试,但是当从测试中使用时,我的视图模型似乎无法工作 具体来说,执行命令似乎不会触发订阅服务器。在下面的测试中,我正在调用AddPlayer命令,但订阅的处理程序未运行: public class NewGameViewModelTests { private NewGameViewModel viewmodel; public NewGameViewModelTests() {
public class NewGameViewModelTests
{
private NewGameViewModel viewmodel;
public NewGameViewModelTests()
{
viewmodel = new NewGameViewModel();
}
[Fact]
public void CanAddUpToSevenPlayers()
{
foreach(var i in Enumerable.Range(1, 7))
{
viewmodel.NewPlayerName = "Player" + i;
viewmodel.AddPlayer.Execute(null);
Assert.Equal(i, viewmodel.Players.Count);
}
}
}
以下是我正在测试的视图模型:
public class NewGameViewModel : ReactiveObject
{
public ReactiveList<string> Players { get; private set; }
public ReactiveCommand<Object> AddPlayer { get; private set; }
public ReactiveCommand<Object> RemovePlayer { get; private set; }
public ReactiveCommand<Object> StartGame { get; private set; }
public ReactiveCommand<Object> RandomizeOrder { get; private set; }
string newPlayerName;
public string NewPlayerName {
get { return newPlayerName; }
set { this.RaiseAndSetIfChanged(ref newPlayerName, value); }
}
public NewGameViewModel()
{
Players = new ReactiveList<string> ();
var canStart = this.Players.CountChanged.Select(count => count >= 3);
StartGame = canStart.ToCommand();
RandomizeOrder = canStart.ToCommand();
RemovePlayer = ReactiveCommand.Create();
AddPlayer = this.WhenAnyValue(x => x.Players.Count, x => x.NewPlayerName,
(count, newPlayerName) => count < 7 && !string.IsNullOrWhiteSpace(newPlayerName) && !this.Players.Contains(newPlayerName))
.ToCommand();
RandomizeOrder.Subscribe(_ =>
{
using (Players.SuppressChangeNotifications())
{
var r = new Random();
var newOrder = Players.OrderBy(x => r.NextDouble()).ToList();
Players.Clear();
Players.AddRange(newOrder);
}
});
RemovePlayer.Subscribe(player =>
{
this.Players.Remove((string)player);
});
AddPlayer.Subscribe(_ =>
{
Players.Add(NewPlayerName.Trim());
NewPlayerName = string.Empty;
});
}
}
public类NewGameViewModel:ReactiveObject
{
公共反应列表播放机{get;private set;}
public ReactiveCommand AddPlayer{get;private set;}
public ReactiveCommand RemovePlayer{get;private set;}
public ReactiveCommand startName{get;private set;}
public ReactiveCommand RandomizeOrder{get;private set;}
字符串newPlayerName;
公共字符串NewPlayerName{
获取{return newPlayerName;}
设置{this.RaiseAndSetIfChanged(ref newPlayerName,value);}
}
public NewGameViewModel()
{
Players=新的反应列表();
var canStart=this.Players.CountChanged.Select(count=>count>=3);
StartGame=canStart.ToCommand();
RandomizeOrder=canStart.ToCommand();
RemovePlayer=ReactiveCommand.Create();
AddPlayer=this.WhenAnyValue(x=>x.Players.Count,x=>x.NewPlayerName,
(count,newPlayerName)=>count<7&&!string.IsNullOrWhiteSpace(newPlayerName)&&!this.Players.Contains(newPlayerName))
.ToCommand();
RandomizeOrder.Subscribe(=>
{
使用(Players.SuppressChangeNotifications())
{
var r=新的随机变量();
var newOrder=Players.OrderBy(x=>r.NextDouble()).ToList();
玩家。清除();
Players.AddRange(newOrder);
}
});
RemovePlayer.Subscribe(播放器=>
{
此.player.Remove((字符串)player);
});
AddPlayer.Subscribe(=>
{
Players.Add(NewPlayerName.Trim());
NewPlayerName=string.Empty;
});
}
}
使用ReactiveUI master在我的机器上似乎没有失败。也许发生了什么事
编辑:这是使用Xamarin.Forms的ReactiveUI中的一个错误。要解决此问题,请将其添加到测试运行开始时运行的地方:
RxApp.MainThreadScheduler = Scheduler.CurrentThread;
您的代码看起来是正确的,我使用RxUI 6.0.1复制并粘贴到我的机器上,测试通过了。我所做的唯一更改是将测试转换为NUnit,这只需要重命名属性和更改断言语法。这里一定发生了什么事情?好的,问题似乎出在reactiveui xamforms 6.0.3上。当我删除测试项目中的引用时,测试开始通过。这是一个错误吗?这是ReactiveUI中的一个错误,感谢您跟踪它。这在ReactiveUI 6.0.4中得到了修复,应该会消失:)这修复了我的测试(ReactiveUI 9.19.5)