Example #1
0
        public void POSTTakeTestReturnsRedirectToAction_WhenModelStateIsValid()
        {
            var fakeHttpContext = new Mock <HttpContext>();
            var fakeIdentity    = new GenericIdentity("User");
            var principal       = new GenericPrincipal(fakeIdentity, null);

            fakeHttpContext.Setup(t => t.User).Returns(principal);

            var tempDataMock = new Mock <ITempDataDictionary>();

            tempDataMock.Setup(x => x["Success - Message"])
            .Returns("Your test was submited!");


            var context = new ControllerContext
            {
                HttpContext = fakeHttpContext.Object
            };


            var id = new Guid().ToString();
            var takeTestRequestVM = new TestRequestViewModel()
            {
                CategoryName = "test",
                Id           = id,
                Questions    = new List <QuestionResponseModel>()
            };

            var testRequestDto = new TestRequestDto();

            this.mapperMock.Setup(x =>
                                  x.MapTo <TestRequestDto>(takeTestRequestVM))
            .Returns(testRequestDto);


            var sut = new HomeController(mapperMock.Object, testServiceMock.Object, userManagerMock.Object,
                                         questionServiceMock.Object, categoryServiceMock.Object, userTestServiceMock.Object,
                                         userAnswerServiceMock.Object)
            {
                //Set your controller ControllerContext with fake context
                ControllerContext = context,
                TempData          = tempDataMock.Object
            };

            // Act
            var result = sut.TakeTest(takeTestRequestVM) as RedirectToActionResult;

            // Assert
            Assert.AreEqual(result.ActionName, "Index");

            //var model = result.Model as TestRequestViewModel;


            //Assert.IsInstanceOfType(result, typeof(ViewResult));
            //Assert.IsNotNull(result);
            //Assert.AreEqual(model.Id, id);
        }
Example #2
0
        public void POSTTakeTestCallsUserTestServiceSubmitTest_WhenModelStateIsValid()
        {
            var fakeHttpContext = new Mock <HttpContext>();
            var fakeIdentity    = new GenericIdentity("User");
            var principal       = new GenericPrincipal(fakeIdentity, null);

            fakeHttpContext.Setup(t => t.User).Returns(principal);

            var tempDataMock = new Mock <ITempDataDictionary>();

            tempDataMock.Setup(x => x["Success - Message"])
            .Returns("Your test was submited!");


            var context = new ControllerContext
            {
                HttpContext = fakeHttpContext.Object
            };


            var id = new Guid().ToString();
            var takeTestRequestVM = new TestRequestViewModel()
            {
                CategoryName = "test",
                Id           = id,
                Questions    = new List <QuestionResponseModel>()
            };

            var testRequestDto = new TestRequestDto();

            this.mapperMock.Setup(x =>
                                  x.MapTo <TestRequestDto>(takeTestRequestVM))
            .Returns(testRequestDto);

            this.userTestServiceMock.Setup(
                x => x.SubmitUserTest(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <bool>()))
            .Verifiable();


            var sut = new HomeController(mapperMock.Object, testServiceMock.Object, userManagerMock.Object,
                                         questionServiceMock.Object, categoryServiceMock.Object, userTestServiceMock.Object,
                                         userAnswerServiceMock.Object)
            {
                //Set your controller ControllerContext with fake context
                ControllerContext = context,
                TempData          = tempDataMock.Object
            };

            // Act
            var result = sut.TakeTest(takeTestRequestVM);

            // Assert
            this.userTestServiceMock.Verify(
                x => x.SubmitUserTest(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <bool>()), Times.Once);
        }
Example #3
0
        public ApiResult Test(TestRequestDto <string> dto)
        {
            var result = Runner.Execute(service.HisInterfaceTest, dto.GetRequestData()).Result;

            if (result.hasError)
            {
                return(new ApiResult <Exception> {
                    Success = false, ResultData = result.error
                });
            }
            else
            {
                return(new ApiResult <string> {
                    Success = true, ResultData = result.data
                });
            }
        }
Example #4
0
        public async Task <IActionResult> TestPost(
            [FromBody] TestRequestDto testRequestDto,
            [FromServices] IMediator mediator
            )
        {
            var request = new TestRequest(
                testRequestDto.Id,
                testRequestDto.Teste);

            var response = await mediator.Send(request);

            if (!response.Success)
            {
                return(BadRequest(response.Errors));
            }

            return(Ok(response.Result));
        }
Example #5
0
        public bool IsTestPassed(string testId, TestRequestDto submitedTest, TestDto testWithQuestions)
        {
            if (string.IsNullOrEmpty(testId))
            {
                throw new ArgumentNullException("Test Id cannot be null!");
            }

            if (submitedTest == null)
            {
                throw new ArgumentNullException(nameof(submitedTest));
            }

            var totalCorrectQuestions = 0;

            for (int i = 0; i < testWithQuestions.Questions.Count; i++)
            {
                var submitedQuestionId = submitedTest.Questions[i].Id;
                var matchingQuestion   = testWithQuestions.Questions
                                         .FirstOrDefault(q => q.Id == submitedQuestionId);

                var correctAnswer = matchingQuestion.Answers
                                    .Where(x => x.IsCorrect == true)
                                    .FirstOrDefault();

                var selectedAnswer = submitedTest.Questions[i].Answers;

                if (selectedAnswer == null)
                {
                    continue;
                }

                if (correctAnswer.Id == selectedAnswer)
                {
                    totalCorrectQuestions++;
                }
            }

            return(this.IsTestPassed(testWithQuestions.Questions.Count(), totalCorrectQuestions));
        }