示例#1
0
        public void CreateTest_Should_Call_Saver_SaveChanges()
        {
            // Arrange
            var dbQuestions = new List <Question>()
            {
                new Question(), new Question(), new Question()
            };
            var questions = new List <AdministerQuestionDto>()
            {
                new AdministerQuestionDto(), new AdministerQuestionDto(), new AdministerQuestionDto()
            };

            var testDto = new AdministerTestDto()
            {
                Category     = "Java",
                TestName     = "Some Test",
                Duration     = 20,
                IsPusblished = false,
                Questions    = questions
            };

            var allCategories = new List <Category>()
            {
                new Category {
                    Id = Guid.NewGuid(), Name = "Java"
                }
            };

            categoryRepoMock.Setup(x => x.All).Returns(allCategories.AsQueryable());
            testRepoMock.Setup(x => x.Add(It.IsAny <Test>())).Verifiable();
            saverMock.Setup(x => x.SaveChanges()).Verifiable();

            mapperMock
            .Setup(x => x.EnumerableProjectTo <AdministerQuestionDto, Question>(It.IsAny <ICollection <AdministerQuestionDto> >()))
            .Verifiable();

            mapperMock
            .Setup(x => x.EnumerableProjectTo <AdministerQuestionDto, Question>(It.IsAny <ICollection <AdministerQuestionDto> >()))
            .Returns(dbQuestions);

            // Act
            testService.CreateTest(testDto);

            // Assert
            saverMock.Verify(x => x.SaveChanges(), Times.Once());
        }
示例#2
0
        public void GetTest_Should_Call_Mapper()
        {
            // Arrange
            var testId   = Guid.NewGuid();
            var category = new Category()
            {
                Name = "Java"
            };
            var answers = new List <Answer>()
            {
                new Answer(), new Answer(), new Answer()
            };
            var questions = new List <Question>()
            {
                new Question()
                {
                    Answers = answers
                }
            };
            var test = new Test()
            {
                Id = testId, Questions = questions, Category = category, TestName = "Test1"
            };
            var all = new List <Test>()
            {
                test
            };

            testRepoMock.Setup(x => x.All).Verifiable();
            testRepoMock.Setup(x => x.All).Returns(all.AsQueryable());
            AdministerTestDto testDto = new AdministerTestDto();

            mapperMock.Setup(x => x.MapTo <AdministerTestDto>(test)).Verifiable();
            mapperMock.Setup(x => x.MapTo <AdministerTestDto>(test)).Returns(testDto);

            // Act
            var result = testService.GetTest("Test1", "Java");

            // Assert
            mapperMock.Verify(x => x.MapTo <AdministerTestDto>(test), Times.Once);
        }
示例#3
0
        public void CreateTest(AdministerTestDto testDto)
        {
            if (testDto == null)
            {
                throw new ArgumentNullException(nameof(testDto));
            }

            Guid category = this.categoryRepo.All
                            .Where(c => c.Name == testDto.Category)
                            .Select(c => c.Id)
                            .SingleOrDefault();

            Test testToBeAdded = new Test()
            {
                TestName     = testDto.TestName,
                CategoryId   = category,
                Duration     = TimeSpan.FromMinutes(testDto.Duration),
                IsPusblished = testDto.IsPusblished,
                Questions    = this.Mapper.EnumerableProjectTo <AdministerQuestionDto, Question>(testDto.Questions).ToList()
            };

            this.testRepo.Add(testToBeAdded);
            Saver.SaveChanges();
        }