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); }
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); }
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); }