public async Task Should_StatusCode201_WhenCreateTodo() { _mockRepo.Setup(repo => repo.Add(GetTestTodo())); _mockRepo.Setup(repo => repo.SaveAll()).ReturnsAsync(true); var controller = new TodoController(_mockRepo.Object, _mockMapper.Object); var result = await controller.CreateTodo(CreateTestTodoDetailedDto()); var statusCodeResult = result as StatusCodeResult; Assert.IsNotNull(statusCodeResult); Assert.AreEqual(201, statusCodeResult.StatusCode); }
public async Task CreateTodo_NullModel_BadRequest() { // Arrange var service = new MockTodoService(); var controller = new TodoController(service); // Act var result = await controller.CreateTodo(null) as BadRequestResult; // Assert Assert.NotNull(result); Assert.Equal(400, result.StatusCode); }
public async Task Create_Item() { var newItem = new TodoItem { Name = "AName", IsComplete = false }; var result = await ControllerUnderTest.CreateTodo(newItem) as CreatedAtRouteResult; var createdItem = result.Value as TodoItem; Assert.AreEqual(createdItem.Id, 2); }
public async Task CreateTodo_InvalidUser_UserNotFoundException() { // Arrange var service = new MockTodoService { MCreateTodoAsync = (model, userId) => throw new UserNotFoundException() }; var controller = new TodoController(service); MockClaims.AddUserIdClaim(controller, MockApplicationUsers.Get(0).Id); // Act var result = await controller.CreateTodo(MockCreateTodoViewModel.Get(0)) as UnauthorizedResult; // Assert Assert.NotNull(result); Assert.Equal(401, result.StatusCode); }
public async Task CreateTodo_ValidUserAndTodo_CreatedAtRoute() { // Arrange var id = 15; var service = new MockTodoService { MCreateTodoAsync = (model, userId) => id }; var controller = new TodoController(service); MockClaims.AddUserIdClaim(controller, MockApplicationUsers.Get(0).Id); // Act var result = await controller.CreateTodo(MockCreateTodoViewModel.Get(0)) as CreatedAtRouteResult; // Assert Assert.NotNull(result); Assert.Equal(201, result.StatusCode); Assert.Equal(MethodNames.GetSingleTodoMethodName, result.RouteName); Assert.Single(result.RouteValues.Keys); Assert.True(result.RouteValues.ContainsKey("todoId")); Assert.Equal(id, result.RouteValues.GetValueOrDefault("todoId")); }
public async Task CreateTodo_InvalidModel_BadRequest() { // Arrange const string field = "required"; const string errorMessage = "something is required"; var service = new MockTodoService(); var controller = new TodoController(service); controller.ModelState.AddModelError(field, errorMessage); var model = MockCreateTodoViewModel.Get(0); // Act var result = await controller.CreateTodo(model) as BadRequestObjectResult; var error = result?.Value as SerializableError; // Assert Assert.NotNull(error); Assert.Equal(400, result.StatusCode); Assert.Single(error.Keys); Assert.True(error.ContainsKey(field)); Assert.Equal(new[] { errorMessage }, error.GetValueOrDefault(field)); }