public void NavigateToNewMealCreatesEmptyMeal() { var loggerMock = new Mock<ILoggerFacade>(); var regionNavigationServiceMock = new Mock<IRegionNavigationService>(); var regionManagerMock = new Mock<IRegionManager>(); var interactionServiceMock = new Mock<IInteractionService>(); var dataRepositoryMock = new Mock<IDataRepository>(); dataRepositoryMock .Setup( x => x.GetAllMealTypes() ) .Returns( new ReadOnlyCollection<MealType>( new List<MealType>() ) ); dataRepositoryMock .Setup( x => x.GetAllFoodItems() ) .Returns( new ReadOnlyCollection<FoodItem>( new List<FoodItem>() ) ); var meal = new Meal( Guid.NewGuid(), new MealType( Guid.NewGuid(), "Test", "", DateTime.Now, false ), DateTime.Now, "Test", "This is a test meal" ); dataRepositoryMock.Setup( x => x.GetMeal( meal.ID ) ).Returns( meal ); var viewModel = new MealViewModel( dataRepositoryMock.Object, regionManagerMock.Object, interactionServiceMock.Object, loggerMock.Object ); var navigationContext = new NavigationContext( regionNavigationServiceMock.Object, new Uri( "MealView", UriKind.Relative ) ); viewModel.OnNavigatedTo( navigationContext ); Assert.AreNotEqual( meal.ID, viewModel.ID ); Assert.IsNull( viewModel.Name ); Assert.IsNull( viewModel.Description ); Assert.AreEqual( 0, viewModel.FoodItemServings.Count ); Assert.IsFalse( viewModel.IsDirty ); }
public void IsNavigationTargetTrueWhenIDMatches() { var loggerMock = new Mock<ILoggerFacade>(); var regionNavigationServiceMock = new Mock<IRegionNavigationService>(); var regionManagerMock = new Mock<IRegionManager>(); var interactionServiceMock = new Mock<IInteractionService>(); var dataRepositoryMock = new Mock<IDataRepository>(); dataRepositoryMock .Setup( x => x.GetAllMealTypes() ) .Returns( new ReadOnlyCollection<MealType>( new List<MealType>() ) ); dataRepositoryMock .Setup( x => x.GetAllFoodItems() ) .Returns( new ReadOnlyCollection<FoodItem>( new List<FoodItem>() ) ); var meal = new Meal( Guid.NewGuid(), new MealType( Guid.NewGuid(), "Test", "", DateTime.Now, false ), DateTime.Now, "Test", "This is a test meal" ); dataRepositoryMock.Setup( x => x.GetMeal( meal.ID ) ).Returns( meal ); var viewModel = new MealViewModel( dataRepositoryMock.Object, regionManagerMock.Object, interactionServiceMock.Object, loggerMock.Object ); var query = new UriQuery(); query.Add( "ID", meal.ID.ToString() ); var navigationContext = new NavigationContext( regionNavigationServiceMock.Object, new Uri( "MealView" + query.ToString(), UriKind.Relative ) ); viewModel.OnNavigatedTo( navigationContext ); Assert.IsTrue( viewModel.IsNavigationTarget( navigationContext ) ); }
/// <summary> /// Close test meal. All of the close tests follow this based pattern, so they all call this rather than repeating everything /// </summary> private void RunCloseTest( Boolean makeDirty, Boolean makeInvalid, MessageBoxResult messageResponse, Boolean expectRemove, Boolean expectSave ) { var loggerMock = new Mock<ILoggerFacade>(); Mock<IDataRepository> dataRepositoryMock = new Mock<IDataRepository>(); Mock<IRegionNavigationService> regionNavigationServiceMock = new Mock<IRegionNavigationService>(); Mock<IRegionManager> regionManagerMock = new Mock<IRegionManager>(); Mock<IRegion> regionWithoutViewMock = new Mock<IRegion>(); Mock<IRegion> regionMock = new Mock<IRegion>(); Mock<IInteractionService> interactionServiceMock = new Mock<IInteractionService>(); // Set up the food group list in the data repository mock. FoodGroup testFoodGroup = new FoodGroup( Guid.NewGuid(), "test", "The only food group in the mock data repository" ); List<FoodGroup> foodGroups = new List<FoodGroup>(); foodGroups.Add( testFoodGroup ); dataRepositoryMock.Setup( x => x.GetAllFoodGroups() ).Returns( new ReadOnlyCollection<FoodGroup>( foodGroups ) ); FoodItem testFoodItem = new FoodItem( Guid.NewGuid(), "Test Food Item", "Test Food Item Description", 42.0M ); testFoodItem.FoodGroupsPerServing.Add( new Serving<FoodGroup>( testFoodGroup, 5.0M ) ); Assert.IsTrue( testFoodItem.IsValid ); List<FoodItem> foodItems = new List<FoodItem>(); foodItems.Add( testFoodItem ); dataRepositoryMock.Setup( x => x.GetAllFoodItems() ).Returns( new ReadOnlyCollection<FoodItem>( foodItems ) ); MealType testMealType = new MealType( Guid.NewGuid(), "Lunch", "A lunch for testing", DateTime.Now, false ); Assert.IsTrue( testMealType.IsValid ); List<MealType> mealTypes = new List<MealType>(); mealTypes.Add( testMealType ); dataRepositoryMock.Setup( x => x.GetAllMealTypes() ).Returns( new ReadOnlyCollection<MealType>( mealTypes ) ); var meal = new Meal( Guid.NewGuid(), testMealType, DateTime.Now, "test meal", "This is a test" ); meal.FoodItemServings.Add( new Serving<FoodItem>( testFoodItem, 1.5M ) ); dataRepositoryMock.Setup( x => x.GetMeal( meal.ID ) ).Returns( meal ); dataRepositoryMock.Setup( x => x.Contains( meal ) ).Returns( true ); // Create the view model under test and associate it with a view var viewModel = new MealViewModel( dataRepositoryMock.Object, regionManagerMock.Object, interactionServiceMock.Object, loggerMock.Object ); UserControl view = new UserControl(); view.DataContext = viewModel; // Set up two regions each with their own set of views. List<UserControl> views = new List<UserControl>(); views.Add( new UserControl() ); views.Add( view ); views.Add( new UserControl() ); views.Add( new UserControl() ); List<UserControl> viewsWithoutView = new List<UserControl>(); viewsWithoutView.Add( new UserControl() ); viewsWithoutView.Add( new UserControl() ); List<IRegion> regions = new List<IRegion>(); regions.Add( regionMock.Object ); regionManagerMock.Setup( x => x.Regions.GetEnumerator() ).Returns( regions.GetEnumerator() ); regionWithoutViewMock.Setup( x => x.Views.GetEnumerator() ).Returns( viewsWithoutView.GetEnumerator() ); regionMock.Setup( x => x.Views.GetEnumerator() ).Returns( views.GetEnumerator() ); // Navigate to the view that "displays" our food foodGroup. This loads the view model NavigationContext navigationContext = new NavigationContext( regionNavigationServiceMock.Object, new Uri( "MealView?ID=" + meal.ID.ToString(), UriKind.Relative ) ); viewModel.OnNavigatedTo( navigationContext ); if (makeDirty) { if (makeInvalid) { interactionServiceMock .Setup( x => x.ShowMessageBox( Messages.Question_Meal_Close, DisplayStrings.CloseCaption, MessageBoxButton.YesNo, MessageBoxImage.Question ) ) .Returns( messageResponse ); viewModel.Name = ""; Assert.IsTrue( viewModel.IsDirty ); Assert.IsFalse( viewModel.IsValid ); } else { interactionServiceMock .Setup( x => x.ShowMessageBox( Messages.Question_Meal_Save, DisplayStrings.SaveChangesCaption, MessageBoxButton.YesNoCancel, MessageBoxImage.Question ) ) .Returns( messageResponse ); viewModel.Name = "Something Else"; Assert.IsTrue( viewModel.IsDirty ); Assert.IsTrue( viewModel.IsValid ); } } else { // This will fail if we have passed in the non-sensical makeDirty == false, makeInvalid == true Assert.AreEqual( makeDirty, viewModel.IsDirty ); } // Attempt a close. viewModel.CloseCommand.Execute( null ); // If we were dirty, then we need to verify that the correct interaction was done, otherwise, that no interaction was done if (makeDirty) { interactionServiceMock.VerifyAll(); } else { interactionServiceMock.Verify( x => x.ShowMessageBox( It.IsAny<String>(), It.IsAny<String>(), It.IsAny<MessageBoxButton>(), It.IsAny<MessageBoxImage>() ), Times.Never() ); } if (expectRemove) { regionMock.Verify( x => x.Remove( view ), Times.Exactly( 1 ) ); regionMock.Verify( x => x.Remove( It.IsAny<UserControl>() ), Times.Exactly( 1 ) ); } else { regionMock.Verify( x => x.Remove( It.IsAny<UserControl>() ), Times.Never() ); } if (expectSave) { dataRepositoryMock.Verify( x => x.SaveItem( meal ), Times.Exactly( 1 ) ); dataRepositoryMock.Verify( x => x.SaveItem( It.IsAny<Meal>() ), Times.Exactly( 1 ) ); } else { dataRepositoryMock.Verify( x => x.SaveItem( It.IsAny<Meal>() ), Times.Never() ); } }
private MealViewModel CreateViewModelForMeal( Meal meal, Mock<IDataRepository> dataRepositoryMock, Mock<IRegionManager> regionManagerMock, Mock<IInteractionService> interactionServiceMock ) { var loggerMock = new Mock<ILoggerFacade>(); var regionNavigationServiceMock = new Mock<IRegionNavigationService>(); var viewModel = new MealViewModel( dataRepositoryMock.Object, regionManagerMock.Object, interactionServiceMock.Object, loggerMock.Object ); dataRepositoryMock.Setup( x => x.GetMeal( meal.ID ) ).Returns( meal ); var navigationContext = new NavigationContext( regionNavigationServiceMock.Object, new Uri( "MealView?ID=" + meal.ID.ToString(), UriKind.Relative ) ); viewModel.OnNavigatedTo( navigationContext ); return viewModel; }
private UserControl CreateViewInRegion( MealViewModel viewModel, Mock<IRegion> regionMock, Mock<IRegionManager> regionManagerMock ) { // Set up region manager so we can determine if the view get removed or not. var view = new UserControl(); view.DataContext = viewModel; var views = new List<UserControl>(); views.Add( new UserControl() ); views.Add( view ); views.Add( new UserControl() ); views.Add( new UserControl() ); var regions = new List<IRegion>(); regions.Add( regionMock.Object ); regionManagerMock.Setup( x => x.Regions.GetEnumerator() ).Returns( regions.GetEnumerator() ); regionMock.Setup( x => x.Views.GetEnumerator() ).Returns( views.GetEnumerator() ); return view; }
private MealViewModel CreateEmptyViewModel( Mock<IDataRepository> dataRepositoryMock ) { var loggerMock = new Mock<ILoggerFacade>(); var regionNavigationServiceMock = new Mock<IRegionNavigationService>(); var regionManagerMock = new Mock<IRegionManager>(); var interactionServiceMock = new Mock<IInteractionService>(); var viewModel = new MealViewModel( dataRepositoryMock.Object, regionManagerMock.Object, interactionServiceMock.Object, loggerMock.Object ); var navigationContext = new NavigationContext( regionNavigationServiceMock.Object, new Uri( "MealView", UriKind.Relative ) ); viewModel.OnNavigatedTo( navigationContext ); return viewModel; }