public async Task Challenge_ReturnsViewError_IfEmptyUser() { // Arrange var mockUserManager = new FakeUserManager(); var mockMapper = new Mock <IMapper>(); var mockLoggerFactory = new Mock <ILoggerFactory>(); var mockLogger = new Mock <ILogger>(); mockLoggerFactory.Setup(l => l.CreateLogger(It.IsAny <string>())).Returns(mockLogger.Object); var mockWorkoutService = new Mock <IWorkoutService>(); mockWorkoutService.Setup(w => w.GetWorkoutEntryAsync(It.IsAny <int>())) .ReturnsAsync((WorkoutEntryDto)null) .Verifiable(); var mockExerciseService = new Mock <IExerciseService>(); var mockChallengeService = new Mock <IChallengeService>(); var mockWorkoutRepository = new Mock <IWorkoutRepository>(); var workoutController = new WorkoutController( mockLoggerFactory.Object, mockMapper.Object, mockWorkoutService.Object, mockExerciseService.Object, mockUserManager, mockWorkoutRepository.Object, mockChallengeService.Object); // Act var result = await workoutController.Challenge(It.IsAny <int>(), It.IsAny <int>()) as ViewResult; // Assert Assert.IsType <ViewResult>(result); Assert.Equal("/Error", result.ViewName); }
public async Task ChallengePost_ReturnsBadRequestResult_WhenModelStateIsInvalid() { // Arrange var challengeViewModel = new NewChallengeViewModel() { Id = 1, ExerciseId = 1, ExerciseName = "Squat", Reps = -10, Set = -10, Weight = -10 }; var mockUserManager = new FakeUserManager(); var mockMapper = new Mock <IMapper>(); var mockLoggerFactory = new Mock <ILoggerFactory>(); var mockLogger = new Mock <ILogger>(); mockLoggerFactory.Setup(l => l.CreateLogger(It.IsAny <string>())).Returns(mockLogger.Object); var mockWorkoutService = new Mock <IWorkoutService>(); var mockExerciseService = new Mock <IExerciseService>(); var mockChallengeService = new Mock <IChallengeService>(); var mockWorkoutRepository = new Mock <IWorkoutRepository>(); var workoutController = new WorkoutController( mockLoggerFactory.Object, mockMapper.Object, mockWorkoutService.Object, mockExerciseService.Object, mockUserManager, mockWorkoutRepository.Object, mockChallengeService.Object); workoutController.ModelState.AddModelError("ExerciseName", "Required"); // Act var result = await workoutController.Challenge(challengeViewModel) as ViewResult; // Assert Assert.IsType <ViewResult>(result); Assert.IsType <NewChallengeViewModel>(result.Model); Assert.Equal(false, result.ViewData.ModelState.IsValid); }
public async Task ChallengePost_ReturnsViewError_ChallengeNotCreated() { // Arrange var newChallengeDto = new NewChallengeDto() { Weight = 10, ChallengeeId = 1, ChallengerId = 2, ExerciseId = 1, Reps = 10 }; var challengeableUserDtos = new List <ChallengeAbleUserDto>() { new ChallengeAbleUserDto { DisplayName = "Test", Id = 1 } }; var challengeViewModel = new NewChallengeViewModel() { Id = 1, ExerciseId = 1, ChallengeAbleUserDtos = challengeableUserDtos, ChallengeeId = challengeableUserDtos.First().Id, ExerciseName = "Squat", Reps = 10, Set = 10, Weight = 10 }; var mockUserManager = new FakeUserManager(); var mockMapper = new Mock <IMapper>(); mockMapper.Setup(m => m.Map <NewChallengeDto>(It.IsAny <NewChallengeViewModel>())) .Returns(newChallengeDto) .Verifiable(); var mockLoggerFactory = new Mock <ILoggerFactory>(); var mockLogger = new Mock <ILogger>(); mockLoggerFactory.Setup(l => l.CreateLogger(It.IsAny <string>())).Returns(mockLogger.Object); var mockWorkoutService = new Mock <IWorkoutService>(); var mockExerciseService = new Mock <IExerciseService>(); var mockChallengeService = new Mock <IChallengeService>(); mockChallengeService.Setup(c => c.CreateChallengeAsync(It.IsAny <NewChallengeDto>())) .ReturnsAsync(0) // Result challenge not created .Verifiable(); var mockWorkoutRepository = new Mock <IWorkoutRepository>(); var workoutController = new WorkoutController( mockLoggerFactory.Object, mockMapper.Object, mockWorkoutService.Object, mockExerciseService.Object, mockUserManager, mockWorkoutRepository.Object, mockChallengeService.Object); // Act var result = await workoutController.Challenge(challengeViewModel) as ViewResult; // Assert Assert.IsType <ViewResult>(result); Assert.Equal("/Error", result.ViewName); }
public async Task Challenge_ReturnsViewResult_WithModel() { // Arrange var workoutEntryDto = new WorkoutEntryDto() { Id = 1, ExerciseId = 1, ExerciseName = "Squat", Reps = 10, Set = 10, Weight = 10 }; var challengeableUserDtos = new List <ChallengeAbleUserDto>() { new ChallengeAbleUserDto { DisplayName = "Test", Id = 1 } }; var challengeViewModel = new NewChallengeViewModel() { Id = 1, ExerciseId = 1, ChallengeAbleUserDtos = challengeableUserDtos, ChallengeeId = challengeableUserDtos.First().Id, ExerciseName = "Squat", Reps = 10, Set = 10, Weight = 10 }; var mockUserManager = new FakeUserManager(); var mockMapper = new Mock <IMapper>(); mockMapper.Setup(m => m.Map <NewChallengeViewModel>(It.IsAny <WorkoutEntryDto>())) .Returns(challengeViewModel) .Verifiable(); var mockLoggerFactory = new Mock <ILoggerFactory>(); var mockLogger = new Mock <ILogger>(); mockLoggerFactory.Setup(l => l.CreateLogger(It.IsAny <string>())).Returns(mockLogger.Object); var mockWorkoutService = new Mock <IWorkoutService>(); mockWorkoutService.Setup(w => w.GetWorkoutEntryAsync(It.IsAny <int>())) .ReturnsAsync(workoutEntryDto) .Verifiable(); var mockExerciseService = new Mock <IExerciseService>(); var mockChallengeService = new Mock <IChallengeService>(); mockChallengeService.Setup(c => c.GetChallengeAbleUsersAsync(It.IsAny <int>())) .ReturnsAsync(challengeableUserDtos) .Verifiable(); var mockWorkoutRepository = new Mock <IWorkoutRepository>(); var workoutController = new WorkoutController( mockLoggerFactory.Object, mockMapper.Object, mockWorkoutService.Object, mockExerciseService.Object, mockUserManager, mockWorkoutRepository.Object, mockChallengeService.Object); // Act var result = await workoutController.Challenge(It.IsAny <int>(), It.IsAny <int>()) as ViewResult; // Assert Assert.IsType <ViewResult>(result); var model = Assert.IsType <NewChallengeViewModel>(result.Model); Assert.Equal(1, model.Id); Assert.Equal(1, model.ExerciseId); Assert.Equal(challengeableUserDtos, model.ChallengeAbleUserDtos); Assert.Equal(1, model.ChallengeeId); Assert.Equal(10, model.Reps); Assert.Equal(10, model.Set); Assert.Equal(10, model.Weight); //Assert.Equal("Challenge", result); }