public async void AddSurvey_ValidObjectPassed_ReturnsOk() { // Arrange var mock = new Mock <ISurveyService>(); mock.Setup(s => s.AddAsync(It.IsAny <SurveyDTO>())).Returns((SurveyDTO s) => FakeServicesMethods.AddSurvey(s)); var controller = new SurveyController(mock.Object); // Act var survey = new SurveyDTO { Id = 1, Creator = "John", Date = DateTime.Now, Views = 3, Title = "Survey 1", Questions = new List <QuestionDTO>() }; var result = await controller.AddSurvey(survey); var okResult = result as OkObjectResult; // Assert Assert.NotNull(okResult); Assert.Equal(200, okResult.StatusCode); }
public async void AddSurvey_ValidObjectPassed_ReturnedResponseHasCreatedItem() { // Arrange var mock = new Mock <ISurveyService>(); mock.Setup(s => s.AddAsync(It.IsAny <SurveyDTO>())).Returns((SurveyDTO s) => FakeServicesMethods.AddSurvey(s)); var controller = new SurveyController(mock.Object); // Act var survey = new SurveyDTO { Id = 1, Creator = "John", Date = DateTime.Now, Views = 3, Title = "Survey 1", Questions = new List <QuestionDTO>() }; var result = await controller.AddSurvey(survey); var okResult = result as OkObjectResult; // Assert var item = Assert.IsAssignableFrom <SurveyDTO>(okResult.Value); Assert.Equal(1, (okResult.Value as SurveyDTO)?.Id); }
public async void AddSurvey_InvalidObjectPassed_ReturnsBadRequest() { // Arrange var mock = new Mock <ISurveyService>(); mock.Setup(s => s.AddAsync(It.IsAny <SurveyDTO>())).Returns((SurveyDTO s) => FakeServicesMethods.AddSurvey(s)); var controller = new SurveyController(mock.Object); // Act var result = await controller.AddSurvey(null); var badRequestResult = result as BadRequestResult; // Assert Assert.NotNull(badRequestResult); Assert.Equal(400, badRequestResult.StatusCode); }