public void Edit_WhenShowingView_ThenTheMatchingWorkoutIsShown()
        {
            // Arrange
            var dbContext = new FakeShoeTrackerDbContext()
                .WithShoe("Foo", _currentUserId)
                .WithShoe("Bar", _currentUserId)
                .WithShoe("Other", "some other user")
                .WithWorkout("some workout", _currentUserId,shoeName:"Foo");

            var controller = new WorkoutController(dbContext)
                .WithAuthenticatedUser("some user", _currentUserId);

            var workout = dbContext.Workouts.First();
            var workoutId = workout.WorkoutId;

            // Act
            var result = controller.Edit(workoutId);

            // Assert
            var viewResult = (ViewResult)result;
            var workoutViewModel = viewResult.CastModel<EditWorkoutViewModel>();

            Assert.That(workoutViewModel.WorkoutId, Is.EqualTo(workoutId));
            Assert.That(workoutViewModel.Name, Is.EqualTo(workout.Name));
            Assert.That(workoutViewModel.WorkoutDate, Is.EqualTo(workout.WorkoutDate));
            Assert.That(workoutViewModel.Distance, Is.EqualTo(workout.Distance));
            Assert.That(workoutViewModel.ShoeUsedId, Is.EqualTo(workout.ShoeUsed.ShoeId));
        }
        public void Edit_WhenSavingValidWorkflow_ThenWorkflowIsSaved()
        {
            // Arrange
            var dbContext = new FakeShoeTrackerDbContext();
            dbContext
                .WithShoe("foo", _currentUserId)
                .WithWorkout("some workout", _currentUserId, shoeName: "foo");
            var controller = new WorkoutController(dbContext)
                .WithAuthenticatedUser("some user", _currentUserId)
                .WithValidModelState();

            var workoutId = dbContext.Workouts.Select(s => s.WorkoutId).First();
            var workoutOnView = ((ViewResult)controller.Edit(workoutId)).CastModel<EditWorkoutViewModel>();

            workoutOnView.Name = "some other name";
            workoutOnView.Distance = 55m;
            workoutOnView.WorkoutDate = DateTime.Today.AddDays(5);

            // Act
            controller.Edit(workoutOnView);

            // Assert
            Assert.That(dbContext.NumerOfTimesSaveChangesCalled, Is.EqualTo(1));

            var savedWorkout = dbContext.Workouts.First(w => w.WorkoutId == workoutId);
            Assert.That(savedWorkout.Name,Is.EqualTo(workoutOnView.Name));
            Assert.That(savedWorkout.Distance,Is.EqualTo(workoutOnView.Distance));
            Assert.That(savedWorkout.WorkoutDate,Is.EqualTo(workoutOnView.WorkoutDate));
        }
        public void Edit_WhenShowingViewWithInvalidWorkoutId_ThenNotFoundResult()
        {
            // Arrange
            var dbContext = new FakeShoeTrackerDbContext();
            var controller = new WorkoutController(dbContext)
                .WithAuthenticatedUser("some user", _currentUserId);

            const int workoutId = -1;

            // Act
            var result = controller.Edit(workoutId);

            // Assert
            Assert.That(result, Is.InstanceOf<HttpNotFoundResult>());
        }
        public void Edit_WhenSavingInValidWorkout_ThenWorkoutIsNotSaved()
        {
            // Arrange
            var dbContext = new FakeShoeTrackerDbContext();
            dbContext
                .WithShoe("foo", _currentUserId)
                .WithWorkout("some workout", _currentUserId, shoeName: "foo");
            var controller = new WorkoutController(dbContext)
                .WithAuthenticatedUser("some user", _currentUserId)
                .WithInvalidModelState();

            var workoutId = dbContext.Workouts.Select(s => s.WorkoutId).First();
            var workoutOnView = ((ViewResult)controller.Edit(workoutId)).CastModel<EditWorkoutViewModel>();

            // Act
            controller.Edit(workoutOnView);

            // Assert
            Assert.That(dbContext.NumerOfTimesSaveChangesCalled, Is.EqualTo(0));
        }