3

我有一个包含算法列表的视图模型。该列表在视图中显示为列表框,用户可以选择其中一种算法。选择算法后,用户可以单击一个按钮,该按钮应在视图模型中执行命令,该命令加载不同的视图,并带有所选算法的详细信息。

我想通过创建一个单元测试并确保导航也能正常工作来对此进行测试。但我想我需要为区域管理器做一些额外的初始化,因为 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");
    }
}

这是测试,任何建议都会有所帮助。谢谢你,纳迪亚

4

0 回答 0