public void AjaxPredictShouldWorkCorrectly()
        {
            var autoMapperConfig = new AutoMapperConfig();
            autoMapperConfig.Execute(typeof(TrainingsController).Assembly);

            var trainingsServiceMock = new Mock<ITrainingsService>();

            var resultDate = DateTime.Now;

            var predictionsServiceMock = new Mock<ITrainingPrediction>();
            predictionsServiceMock.Setup(
                x =>
                x.Predict(It.IsAny<Training>()))
                .Returns(new Training()
                {
                    EndDate = resultDate
                });

            var mock = new Mock<ControllerContext>();
            mock.SetupGet(p => p.HttpContext.User.Identity.Name).Returns("anon");
            mock.SetupGet(p => p.HttpContext.Request.IsAuthenticated).Returns(true);

            var controller = new TrainingsController(trainingsServiceMock.Object, predictionsServiceMock.Object);
            controller.ControllerContext = mock.Object;
            controller.WithCallTo(x => x.AjaxPredict(new TrainingAjaxPredictViewModel()))
                .ShouldReturnJson(
                x =>
                {
                    var content = x as TrainingAjaxPredictViewModel;
                    Assert.AreEqual(content.EndDate, resultDate);
                });
        }
        public void AjaxLoadNextTrainingsShouldWorkCorrectly()
        {
            var caloriesBurned = 1500;

            var autoMapperConfig = new AutoMapperConfig();
            autoMapperConfig.Execute(typeof(TrainingsController).Assembly);

            var trainingsServiceMock = new Mock<ITrainingsService>();

            trainingsServiceMock.Setup(
                x =>
                x.GetAllByUserWithPagingAndFiltering(It.IsAny<string>(), It.IsAny<int>(), It.IsAny<int>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>()))
                .Returns(new List<Training>()
                {
                    new Training()
                    {
                        Calories = caloriesBurned,
                        Track = new Track()
                        {
                            Ascent = 2500,
                            Length = 50
                        }
                    }
                }.AsQueryable());

            var predictionsServiceMock = new Mock<ITrainingPrediction>();

            //predictionsServiceMock.Setup()

            var mock = new Mock<ControllerContext>();
            mock.SetupGet(p => p.HttpContext.User.Identity.Name).Returns("anon");
            mock.SetupGet(p => p.HttpContext.Request.IsAuthenticated).Returns(true);

            var controller = new TrainingsController(trainingsServiceMock.Object, predictionsServiceMock.Object);
            controller.ControllerContext = mock.Object;
            controller.WithCallTo(x => x.AjaxLoadNextTrainings(null, null, 0, 15))
                .ShouldRenderPartialView("_TrainingsList")
                .WithModel<List<TrainingListItemViewModel>>(
                    viewModel =>
                    {
                        Assert.AreEqual(1, viewModel.Count);
                    }).AndNoModelErrors();
        }
        public void EditShouldWorkCorrectlyWithValidModelState()
        {
            var caloriesBurned = 1500;

            var autoMapperConfig = new AutoMapperConfig();
            autoMapperConfig.Execute(typeof(TrainingsController).Assembly);

            var trainingsServiceMock = new Mock<ITrainingsService>();

            var training = new TrainingEditViewModel()
            {
                Calories = caloriesBurned,
            };

            var predictionsServiceMock = new Mock<ITrainingPrediction>();

            //predictionsServiceMock.Setup()

            var mock = new Mock<ControllerContext>();
            mock.SetupGet(p => p.HttpContext.User.Identity.Name).Returns("anon");
            mock.SetupGet(p => p.HttpContext.Request.IsAuthenticated).Returns(true);

            var controller = new TrainingsController(trainingsServiceMock.Object, predictionsServiceMock.Object);
            controller.ControllerContext = mock.Object;
            controller.WithCallTo(x => x.Edit(training))
                .ShouldRedirectToRoute("");
        }
        public void EditShouldWorkCorrectlyWithInvalidModelState()
        {
            var caloriesBurned = 1500;

            var autoMapperConfig = new AutoMapperConfig();
            autoMapperConfig.Execute(typeof(TrainingsController).Assembly);

            var trainingsServiceMock = new Mock<ITrainingsService>();

            var training = new TrainingEditViewModel()
            {
                Calories = caloriesBurned,
            };

            var predictionsServiceMock = new Mock<ITrainingPrediction>();

            //predictionsServiceMock.Setup()

            var mock = new Mock<ControllerContext>();
            mock.SetupGet(p => p.HttpContext.User.Identity.Name).Returns("anon");
            mock.SetupGet(p => p.HttpContext.Request.IsAuthenticated).Returns(true);

            var controller = new TrainingsController(trainingsServiceMock.Object, predictionsServiceMock.Object);
            controller.ControllerContext = mock.Object;
            controller.ModelState.AddModelError("FirstName", "First Name is Required");
            controller.WithCallTo(x => x.Edit(training))
                .ShouldRenderView("Edit")
                .WithModel<TrainingEditViewModel>(
                    viewModel =>
                    {
                        Assert.AreEqual(caloriesBurned, viewModel.Calories);
                    });
        }
        public void EditShouldWorkCorrectly()
        {
            var caloriesBurned = 1500;

            var autoMapperConfig = new AutoMapperConfig();
            autoMapperConfig.Execute(typeof(TrainingsController).Assembly);

            var trainingsServiceMock = new Mock<ITrainingsService>();

            trainingsServiceMock.Setup(
                x =>
                x.GetById(It.IsAny<object>()))
                .Returns(
                    new Training()
                    {
                        Calories = caloriesBurned,
                        Track = new Track()
                        {
                            Ascent = 2500,
                            Length = 50
                        }
                    }
                );

            var predictionsServiceMock = new Mock<ITrainingPrediction>();

            //predictionsServiceMock.Setup()

            var mock = new Mock<ControllerContext>();
            mock.SetupGet(p => p.HttpContext.User.Identity.Name).Returns("anon");
            mock.SetupGet(p => p.HttpContext.Request.IsAuthenticated).Returns(true);

            var controller = new TrainingsController(trainingsServiceMock.Object, predictionsServiceMock.Object);
            controller.ControllerContext = mock.Object;
            controller.WithCallTo(x => x.Edit(It.IsAny<int>()))
                .ShouldRenderView("Edit")
                .WithModel<TrainingEditViewModel>(
                    viewModel =>
                    {
                        Assert.AreEqual(caloriesBurned, viewModel.Calories);
                    }).AndNoModelErrors();
        }
        public void DeleteShouldWorkCorrectly()
        {
            var autoMapperConfig = new AutoMapperConfig();
            autoMapperConfig.Execute(typeof(TrainingsController).Assembly);

            var trainingsServiceMock = new Mock<ITrainingsService>();

            var predictionsServiceMock = new Mock<ITrainingPrediction>();

            var mock = new Mock<ControllerContext>();
            mock.SetupGet(p => p.HttpContext.User.Identity.Name).Returns("anon");
            mock.SetupGet(p => p.HttpContext.Request.IsAuthenticated).Returns(true);

            var controller = new TrainingsController(trainingsServiceMock.Object, predictionsServiceMock.Object);
            controller.ControllerContext = mock.Object;
            controller.WithCallTo(x => x.Delete(It.IsAny<int>()))
                .ShouldRedirectToRoute("");
        }
        public void CreateShouldRedirectToHomeWhenValidModel()
        {
            var autoMapperConfig = new AutoMapperConfig();
            autoMapperConfig.Execute(typeof(TrainingsController).Assembly);

            var trainingsServiceMock = new Mock<ITrainingsService>();

            var predictionsServiceMock = new Mock<ITrainingPrediction>();

            var mock = new Mock<ControllerContext>();
            mock.SetupGet(p => p.HttpContext.User.Identity.Name).Returns("anon");
            mock.SetupGet(p => p.HttpContext.Request.IsAuthenticated).Returns(true);

            var controller = new TrainingsController(trainingsServiceMock.Object, predictionsServiceMock.Object);
            controller.ControllerContext = mock.Object;
            controller.WithCallTo(x => x.Create(new TrainingCreateViewModel()
            {
                Calories = 1500,
                EndDate = DateTime.Now,
                StartDate = DateTime.Now,
                Title = "Test",
                Water = 3,
                Track = new TrackCreateViewModel()
                {
                    Ascent = 3000,
                    AscentLength = 25,
                    Length = 80,
                    Title = "test track 1"
                },
            }))
                .ShouldRedirectToRoute("");
        }
        public void CreateShouldRedirectToCreateWhenInvalidModel()
        {
            var autoMapperConfig = new AutoMapperConfig();
            autoMapperConfig.Execute(typeof(TrainingsController).Assembly);

            var trainingsServiceMock = new Mock<ITrainingsService>();

            var predictionsServiceMock = new Mock<ITrainingPrediction>();

            var mock = new Mock<ControllerContext>();
            mock.SetupGet(p => p.HttpContext.User.Identity.Name).Returns("anon");
            mock.SetupGet(p => p.HttpContext.Request.IsAuthenticated).Returns(true);

            var controller = new TrainingsController(trainingsServiceMock.Object, predictionsServiceMock.Object);
            controller.ControllerContext = mock.Object;
            controller.ModelState.AddModelError("FirstName", "First Name is Required");
            controller.WithCallTo(x => x.Create(new TrainingCreateViewModel()))
                .ShouldRenderView("Create");
        }