我有一个视图模型,其中包含算法列表。该列表在 View 中显示为 ListBox,并且用户可以选择其中一个算法。选择算法后,用户可以单击按钮,执行视图模型中的命令,加载所选算法的详细信息的不同视图。
我想通过创建单元测试来测试这个功能,并确保导航也可以正常工作。但我猜想需要对区域管理器进行一些额外的初始化,因为 IRegionManager.Regions 集合为 null,并且由于它是只读的,我无法创建它。
我想通过创建单元测试来测试这个功能,并确保导航也可以正常工作。但我猜想需要对区域管理器进行一些额外的初始化,因为 IRegionManager.Regions 集合为 null,并且由于它是只读的,我无法创建它。
[TestClass]
public class MockingAlgorithmsTests
{
[TestMethod]
public void AlgorithmVM_LoadSelectedAlgorithmCommand()
{
Mock<IRegionManager> regionManagerMock = new Mock<IRegionManager>();
Mock<IEventAggregator> eventAgregatorMock = new Mock<IEventAggregator>();
IAlgorithmService algorithmService = new MockingAlgorithmService();
AlgorithmsViewModel algorithmsVM = new AlgorithmsViewModel(regionManagerMock.Object, eventAgregatorMock.Object, algorithmService);
// select algorithm
algorithmsVM.SelectedAlgorithm = algorithmsVM.Algorithms.First();
// execute command which uses the previous selected algorithm
// and navigates to a different view
algorithmsVM.LoadSelectedAlgorithmCommand.Execute(null);
// check that the navigation worked and the new view is the one
// which shows the selected algorithm
var enumeratorMainRegion = regionManagerMock.Object.Regions["MainContentRegion"].ActiveViews.GetEnumerator();
enumeratorMainRegion.MoveNext();
var viewFullName = enumeratorMainRegion.Current.ToString();
Assert.AreEqual(viewFullName, "TestApp.AlgorithmViews.AlgorithmDetails");
}
}
这是一个测试,任何建议都会很有帮助。谢谢,Nadia