Пример #1
0
        public void Should_RepoAddExercise_BeCalled_WhenServiceAddExerciseCalled()
        {
            var exerciseType = new ExerciseType()
            {
                Id = 1, Name = "dummy Exercise Type"
            };

            var exerciseVm = new NewExerciseVm()
            {
                Id             = 1,
                Name           = "Dummy",
                ExerciseTypeId = exerciseType.Id,
            };

            var exerciseRepo     = new Mock <IExerciseRepository>();
            var exerciseTypeRepo = new Mock <IExerciseTypeRepository>();
            var mapper           = new Mock <IMapper>();

            exerciseTypeRepo.Setup(r => r.GetExerciseTypeById(exerciseType.Id)).Returns(exerciseType);

            var objectUnderTest = new ExerciseService(exerciseRepo.Object, exerciseTypeRepo.Object, mapper.Object);

            var result = objectUnderTest.AddExercise(exerciseVm);

            exerciseRepo.Verify(r => r.AddExercise(It.IsAny <Exercise>()), Times.Once);

            Assert.Equal(exerciseType.Id, result);
        }
Пример #2
0
        public async Task AddExercise_PersistsExercise()
        {
            var exercise = new Exercise
            {
                Title = "Exercise Title",
                Body  = "Exercise Body"
            };

            using (var context = new ApplicationDbContext(Options))
            {
                var service = new ExerciseService(context);

                Exercise addedExercise = await service.AddExercise(exercise);

                Assert.AreEqual(addedExercise, exercise);
                Assert.AreNotEqual(0, addedExercise.Id);
            }

            using (var context = new ApplicationDbContext(Options))
            {
                Exercise retrievedExercise = context.Exercises.Single();
                Assert.AreEqual(exercise.Title, retrievedExercise.Title);
                Assert.AreEqual(exercise.Body, retrievedExercise.Body);
            }
        }
Пример #3
0
        public async Task <IActionResult> Exercise(Exercise model)
        {
            if (ModelState.IsValid)
            {
                var currentUser = await _userManager.GetUserAsync(User);

                // make sure current user exist
                if (currentUser == null)
                {   // make them login
                    return(Challenge());
                }

                bool success = await _exerciseService.AddExercise(model, currentUser);

                if (!success)
                {
                    return(BadRequest("Could not add the exercise"));
                }

                ModelState.Clear();
                return(View());
            }

            // pass in model to redisplay form with users perviously enterered info
            return(View(model));
        }
Пример #4
0
        public void AddExercise_ShouldThrowWhenInvalidExerciseIsPassed()
        {
            var exercisesRepoStub = new Mock <IEfRepostory <Exercise> >();
            var unitOfWorkStub    = new Mock <IUnitOfWork>();

            var sut = new ExerciseService(exercisesRepoStub.Object, unitOfWorkStub.Object);

            exercisesRepoStub.Setup(x => x.Add(It.IsAny <Exercise>()));
            unitOfWorkStub.Setup(x => x.Commit());

            Assert.Throws <ArgumentException>(() => sut.AddExercise(null));
        }
Пример #5
0
        public void AddExercise_ShouldCallUnitOfWorkCommitOnce()
        {
            var exercisesRepoStub = new Mock <IEfRepostory <Exercise> >();
            var unitOfWorkStub    = new Mock <IUnitOfWork>();

            var sut = new ExerciseService(exercisesRepoStub.Object, unitOfWorkStub.Object);

            exercisesRepoStub.Setup(x => x.Update(It.IsAny <Exercise>()));
            unitOfWorkStub.Setup(x => x.Commit());

            var exercise = new Exercise();

            sut.AddExercise(exercise);

            unitOfWorkStub.Verify(x => x.Commit(), Times.Once);
        }