public async Task GetTodo_Existing_OkWithDto() { // Arrange var service = new MockTodoService { MGetTodoByIdAsync = (todoId, userId) => MockTodoDto.Get(0) }; var controller = new TodoController(service); MockClaims.AddUserIdClaim(controller, MockApplicationUsers.Get(5).Id); const int tId = 10101; // Act var result = await controller.GetTodo(tId) as OkObjectResult; var dto = result?.Value as TodoDto; // Assert Assert.NotNull(result); Assert.Equal(200, result.StatusCode); Assert.NotNull(dto); Assert.Equal(MockTodoDto.Get(0).Id, dto.Id); Assert.Equal(MockTodoDto.Get(0).Due, dto.Due); Assert.Equal(MockTodoDto.Get(0).Description, dto.Description); }
protected override void Context() { _todoServiceMock = new Mock <ITodoService>(); _todoServiceMock.Setup(s => s.GetTodoAsync(1)) .ReturnsAsync(new TodoItem()); _sut = new TodoController(_todoServiceMock.Object); _todoItemResult = _sut.GetTodo(1).Result; }
protected override void Context() { // Arrange _todoServiceMock = new Mock <ITodoService>(); _sut = new TodoController(_todoServiceMock.Object); // Act _todoItemResult = _sut.GetTodo(1).Result; }
public void GetNonExistingTodo() { // Arrange int id = 6; var mockManager = new Mock <ITodoManager>(); mockManager.Setup(i => i.Get(id)).Returns(GetTestTodos().FirstOrDefault(i => i.Id == id)); var controller = new TodoController(mockManager.Object); // Act var result = controller.GetTodo(id); // Assert var objectResult = Assert.IsType <NotFoundResult>(result); }
public void GetExistingTodo() { // Arrange int id = 1; var mockManager = new Mock <ITodoManager>(); mockManager.Setup(i => i.Get(id)).Returns(GetTestTodos().FirstOrDefault(i => i.Id == id)); var controller = new TodoController(mockManager.Object); // Act var result = controller.GetTodo(id); // Assert var objectResult = Assert.IsType <ObjectResult>(result); var todo = Assert.IsAssignableFrom <Todo>(objectResult.Value); }
public async Task GetTodo_WhenNotFound_Returns404() { // Arrange var todoServiceMock = new Mock <ITodoService>(); var todoController = new TodoController(todoServiceMock.Object); todoServiceMock .Setup(s => s.GetByIdAsync(It.IsAny <int>())) .Returns(Task.FromResult <Todo>(null)); // Act var result = await todoController.GetTodo(1); // Assert Assert.IsAssignableFrom <NotFoundResult>(result.Result); Assert.Null(result.Value); }
public async Task CanGetATodo() { var options = new DbContextOptionsBuilder <HoneydewContext>() .UseInMemoryDatabase(databaseName: "testing") .Options; using (var context = new HoneydewContext(options)) { var tController = new TodoController(context); Todolist example = new Todolist() { Name = "example", Id = 9 }; Todo Example = await context.Todos.FirstOrDefaultAsync(x => x.Name == "example"); var result = tController.GetTodo(example.Id); Assert.NotNull(result); } }
public async Task GetTodo_NonExisting_TodoNotFoundException() { // Arrange var service = new MockTodoService { MGetTodoByIdAsync = (todoId, userId) => throw new TodoNotFoundException() }; var controller = new TodoController(service); MockClaims.AddUserIdClaim(controller, "I am just gonna throw an exception anyway"); const int tId = 665; // Act var result = await controller.GetTodo(tId) as NotFoundResult; // Assert Assert.NotNull(result); Assert.Equal(404, result.StatusCode); }
public async Task GetTodo_CallsGetByIdAsync() { // Arrange var todoServiceMock = new Mock <ITodoService>(); var todoController = new TodoController(todoServiceMock.Object); var todo = GetTodo(); todoServiceMock .Setup(s => s.GetByIdAsync(It.IsAny <int>())) .Returns(Task.FromResult(todo)); // Act var result = await todoController.GetTodo(todo.Id); // Assert todoServiceMock.Verify(s => s.GetByIdAsync(It.Is <int>(id => id == todo.Id)), Times.Once()); Assert.IsAssignableFrom <Todo>(result.Value); Assert.Equal(todo, result.Value); }