コード例 #1
0
        public async void AddQuestion_ValidObjectPassed_ReturnsOk()
        {
            // Arrange
            var mock = new Mock <IQuestionService>();

            mock.Setup(s => s.AddAsync(It.IsAny <QuestionDTO>())).Returns((QuestionDTO s) => FakeServicesMethods.AddQuestion(s));
            var controller = new QuestionController(mock.Object);

            // Act
            var question = new QuestionDTO
            {
                Id           = 1,
                Title        = "Question 1",
                QuestionText = "text",
                Comment      = "",
                Answers      = new List <AnswerDTO>()
            };
            var result = await controller.AddQuestion(question);

            var okResult = result as OkObjectResult;

            // Assert
            Assert.NotNull(okResult);
            Assert.Equal(200, okResult.StatusCode);
        }
コード例 #2
0
        public async void AddQuestion_ValidObjectPassed_ReturnedResponseHasCreatedItem()
        {
            // Arrange
            var mock = new Mock <IQuestionService>();

            mock.Setup(s => s.AddAsync(It.IsAny <QuestionDTO>())).Returns((QuestionDTO s) => FakeServicesMethods.AddQuestion(s));
            var controller = new QuestionController(mock.Object);

            // Act
            var question = new QuestionDTO
            {
                Id           = 1,
                Title        = "Question 1",
                QuestionText = "text",
                Comment      = "",
                Answers      = new List <AnswerDTO>()
            };
            var result = await controller.AddQuestion(question);

            var okResult = result as OkObjectResult;

            // Assert
            var item = Assert.IsAssignableFrom <QuestionDTO>(okResult.Value);

            Assert.Equal(1, (okResult.Value as QuestionDTO)?.Id);
        }
コード例 #3
0
        public async void AddQuestion_InvalidObjectPassed_ReturnsBadRequest()
        {
            // Arrange
            var mock = new Mock <IQuestionService>();

            mock.Setup(s => s.AddAsync(It.IsAny <QuestionDTO>())).Returns((QuestionDTO s) => FakeServicesMethods.AddQuestion(s));
            var controller = new QuestionController(mock.Object);

            // Act
            var result = await controller.AddQuestion(null);

            var badRequestResult = result as BadRequestResult;

            // Assert
            Assert.NotNull(badRequestResult);
            Assert.Equal(400, badRequestResult.StatusCode);
        }