Exemplo n.º 1
0
        public async Task <IActionResult> CheckUserAnswers([FromBody] UserAnswersModel model)
        {
            if (model?.Answers is null)
            {
                return(BadRequest("Here is no answers"));
            }

            string userId = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;
            var    testResultCreateDto = mapper.Map <TestResultCreateDto>(model);

            var result = await testResultService.AddResult(userId, testResultCreateDto);

            return(responseComposer.ComposeForCheckUserAnswers(result));
        }
        public async Task AddResultTest()
        {
            unitOfWorksMock
            .Setup(u => u.Tests.GetAsync(It.IsAny <int>()))
            .ReturnsAsync((int id) => new Test
            {
                Id        = id,
                Questions = new List <Question>()
            });

            unitOfWorksMock
            .Setup(u => u.Answers.GetCorrectAnswersForTest(It.IsAny <int>()))
            .ReturnsAsync((int id) => new []
            {
                new Answer
                {
                    Id         = 1,
                    QuestionId = 1,
                    IsCorrect  = true
                },
                new Answer
                {
                    Id         = 5,
                    QuestionId = 2,
                    IsCorrect  = true
                }
            });

            var testResultCreateDto = new TestResultCreateDto
            {
                TestId  = 1,
                UserId  = "user_id",
                Answers = new []
                {
                    new KeyValuePair <int, int>(1, 1),
                    new KeyValuePair <int, int>(2, 5)
                }
            };

            double expectedResult = 100;

            unitOfWorksMock
            .Setup(u => u.TestResults.Add(It.IsAny <TestResult>()))
            .Callback((TestResult testResult) => Assert.AreEqual(expectedResult, testResult.Result, 0.001));

            var result = await testResultService.AddResult("user_id", testResultCreateDto);

            Assert.IsTrue(result.Success);

            testResultCreateDto.Answers = new[]
            {
                new KeyValuePair <int, int>(1, 1),
                new KeyValuePair <int, int>(2, 2)
            };
            expectedResult = 50;
            result         = await testResultService.AddResult("user_id", testResultCreateDto);

            Assert.IsTrue(result.Success);

            testResultCreateDto.Answers = new KeyValuePair <int, int> [0];
            expectedResult = 0;
            result         = await testResultService.AddResult("user_id", testResultCreateDto);

            Assert.IsTrue(result.Success);
        }