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