I have a view model that contains a list of algorithms. The list is displayed in the View as a ListBox and the user can select one of the algorithms. After an algorithm is selected, the user can click on a button that should execute a command in the view model that loads a different view, with the selected algorithm's details.
I want to test this by creating an unit test and assuring that the navigation works as well. But I guess I need to do some extra initialization for the region manager, because the IRegionManager.Regions collection is null and since it is read-only, I cannot create it.
[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");
}
}
This is the test, any suggestion would be helpful. Thank you, Nadia
I was facing same issue for sometime, then I have mocked everything which was needed to region manager. Please see below code snippet if this helps you.
var regionManager = new Mock<IRegionManager>();
var navigationJournal = new Mock<IRegionNavigationJournal>();
navigationJournal.Setup(x => x.GoBack()).Callback(() =>
{
// Verify back operation performed
});
var regionNavigationService = new Mock<IRegionNavigationService>();
regionNavigationService.Setup(x => x.Journal).Returns(navigationJournal.Object);
var navRegionMock = new Mock<IRegion>();
navRegionMock.Setup(x => x.NavigationService).Returns(regionNavigationService.Object);
regionManager.SetupGet(x => x.Regions[RegionNames.ShellContentRegion]).Returns(navRegionMock.Object);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.