public void All_WhenShowingView_ThenAllShoesAreDisplayed()
        {
            // Arrange
            var dbContext = new FakeShoeTrackerDbContext();
            var controller = new ShoeController(dbContext);

            IndexSetup(dbContext, controller);

            // Act
            var result = controller.All();

            // Assert
            var viewModels = result.CastModel<ICollection<ShoeDetailViewModel>>();

            AssertAllShoesAreShown(viewModels,dbContext.Shoes);

            foreach (var viewModel in viewModels)
            {
                var matchingShoe = dbContext.Shoes.First(s => s.ShoeId == viewModel.ShoeId);

                AssertCurrentUser(matchingShoe);
                AssertShoeName(viewModel, matchingShoe);
                AssertFirstUsed(viewModel, matchingShoe);
                AssertLastUsed(viewModel, matchingShoe);
                AssertShoesWithWorkoutsCannotBeDeleted(viewModel, matchingShoe);
                AssertShoesWithWorkoutsCannotBeDeleted(viewModel, matchingShoe);
                AssertDistanceUsedIsSumOfWorkouts(viewModel, matchingShoe);
                AssertPercentRemainigIsRatioOfDistanceToWorkouts(viewModel, matchingShoe);
                AssertExpirationDateIsEmptyWhenTheShoeIsNoLongerUsed(viewModel, matchingShoe);
            }
        }
        public void Create_WhenSavingValidShoe_ThenShoeIsSaved()
        {
            // Arrange
            var dbContext = new FakeShoeTrackerDbContext();
            var controller = new ShoeController(dbContext)
                .WithAuthenticatedUser("some user", _currentUserId);

            var shoeOnView = controller.Create().CastModel<Shoe>();

            // Act
            controller.Create(shoeOnView);

            // Assert
            Assert.That(dbContext.Shoes.Count(), Is.EqualTo(1));
            Assert.That(dbContext.Shoes.First().UserId,Is.EqualTo(_currentUserId));
        }
        public void Create_WhenShowingView_ThenNewShoeHasDefaults()
        {
            // Arrange
            var dbContext = new FakeShoeTrackerDbContext();
            var controller = new ShoeController(dbContext)
                .WithAuthenticatedUser("some user",_currentUserId);

            // Act
            var result = controller.Create();

            // Assert
            var modelData = result.CastModel<Shoe>();

            Assert.That(modelData.UserId,Is.EqualTo(_currentUserId));
            Assert.That(modelData.FirstUsed,Is.EqualTo(DateTime.Today));
        }
        private void IndexSetup(ShoeTrackerDbContext dbContext, ShoeController controller)
        {
            const string username = "******";

            dbContext
                .WithShoe("Foo", _currentUserId)
                .WithShoe("Bar", _currentUserId)
                .WithShoe("BarOther", "otherUser")
                .WithShoe("My Shoe", _currentUserId, lastUsedDate: DateTime.Now.AddDays(-1),maximumDistance:5);

            dbContext
                .WithWorkout("Workout 1", _currentUserId, shoeName: "Bar")
                .WithWorkout("Workout Old", _currentUserId, shoeName: "My Shoe",distance:4);

            controller
                .WithAuthenticatedUser(username, _currentUserId)
                .WithValidModelState();
        }
        public void Edit_WhenShowingView_ThenTheMatchingShoeIsShown()
        {
            // Arrange
            var dbContext = new FakeShoeTrackerDbContext()
                .WithShoe("some shoe", _currentUserId);
            var controller = new ShoeController(dbContext)
                .WithAuthenticatedUser("some user", _currentUserId);

            var shoeId = dbContext.Shoes.Select(s => s.ShoeId).First();

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

            // Assert
            var viewResult = (ViewResult) result;
            var shoe = viewResult.CastModel<Shoe>();

            Assert.That(shoe.ShoeId,Is.EqualTo(shoeId));
        }
        public void Edit_WhenShowingViewWithInvalidShoeId_ThenNotFoundResult()
        {
            // Arrange
            var dbContext = new FakeShoeTrackerDbContext();
            var controller = new ShoeController(dbContext)
                .WithAuthenticatedUser("some user", _currentUserId);

            const int shoeId = -1;

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

            // Assert
            Assert.That(result,Is.InstanceOf<HttpNotFoundResult>());
        }
        public void Edit_WhenSavingValidShoe_ThenShoeIsSaved()
        {
            // Arrange
            var dbContext = new FakeShoeTrackerDbContext();
            dbContext
                .WithShoe("foo", _currentUserId);
            var controller = new ShoeController(dbContext)
                .WithAuthenticatedUser("some user", _currentUserId)
                .WithValidModelState();

            var shoeId = dbContext.Shoes.Select(s => s.ShoeId).First();
            var shoeOnView = ((ViewResult)controller.Edit(shoeId)).CastModel<Shoe>();

            shoeOnView.Name = "some other name";

            // Act
            controller.Edit(shoeOnView);

            // Assert
            Assert.That(dbContext.NumerOfTimesSaveChangesCalled, Is.EqualTo(1));
        }
        public void DeleteConfirmed_WhenDeleting_ThenTheShoeIsRemovedFromTheDatabase()
        {
            // Arrange
            var dbContext = new FakeShoeTrackerDbContext();
            dbContext
                .WithShoe("foo", _currentUserId)
                .WithShoe("other", _currentUserId);
            var controller = new ShoeController(dbContext)
                .WithAuthenticatedUser("some user", _currentUserId)
                .WithValidModelState();

            var shoeId = dbContext.Shoes.Select(s => s.ShoeId).First();

            // Act
            controller.DeleteConfirmed(shoeId);

            // Assert
            Assert.That(dbContext.Shoes.Count(),Is.EqualTo(1));
            Assert.That(dbContext.Shoes.First().Name,Is.EqualTo("other"));
            Assert.That(dbContext.NumerOfTimesSaveChangesCalled, Is.EqualTo(1));
        }
        public void DeleteConfirmed_WhenDeletingInValidShoe_ThenNothingHappes()
        {
            // Arrange
            var dbContext = new FakeShoeTrackerDbContext();
            var controller = new ShoeController(dbContext)
                .WithAuthenticatedUser("some user", _currentUserId)
                .WithValidModelState();

            const int shoeId = -1;

            // Act
            controller.DeleteConfirmed(shoeId);

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