async Task CreateTodo() { Cli.DisplayLine("called CreateTodos()"); Cli.DisplayLine(); var category = await CatCtrl.GetAll(); // this is creating an instance of catergory for belows use var todo = new Todo(); // this is creating a instance of ToDo. todo.Id = 0; todo.Description = Cli.GetString("Enter Description"); todo.Due = Cli.GetDateTime("Enter Due Date"); todo.Note = Cli.GetString("Enter note"); Cli.DisplayLine("Categories"); foreach (var c in category) { Cli.DisplayLine($" {c.Id} : {c.Name}"); } todo.CategoryId = Cli.Getint("Select category"); var newTodo = await ToDoCtrl.Create(todo); Cli.DisplayLine(); Cli.DisplayLine(); //todo.CategoryId = Cli.Getint("1 : Personal \n 2 : Professional"); await ToDoCtrl.Create(todo); // this line is adding the ToDo object to the database }
public void CanReadTodoItem() { DbContextOptions <TodoDbContext> options = new DbContextOptionsBuilder <TodoDbContext>() .UseInMemoryDatabase("Test2Database").Options; using (TodoDbContext context = new TodoDbContext(options)) { // Arrange TodoItem datItem1 = new TodoItem(); datItem1.ID = 1; datItem1.Name = "walk the dog"; datItem1.IsComplete = false; TodoItem datItem2 = new TodoItem(); datItem2.ID = 2; datItem2.Name = "wash the dishes"; datItem2.IsComplete = true; TodoController ic = new TodoController(context); // Act var created1 = ic.Create(datItem1); var created2 = ic.Create(datItem2); var result1 = ic.GetById(datItem1.ID); var result2 = ic.GetById(datItem2.ID); // Assert Assert.Equal(datItem1.ID, result1.Result.Value.ID); Assert.Equal(datItem2.ID, result2.Result.Value.ID); } }
public async void CanRemoveItemsFromList() { DbContextOptions <TodoDbContext> options = new DbContextOptionsBuilder <TodoDbContext>() .UseInMemoryDatabase("Test10Database").Options; using (TodoDbContext context = new TodoDbContext(options)) { // Arrange TodoList datList1 = new TodoList(); datList1.ID = 1; datList1.Name = "house chores"; TodoListController lc = new TodoListController(context); TodoItem datItem1 = new TodoItem(); datItem1.ID = 30; datItem1.Name = "vacuum the floor"; datItem1.IsComplete = false; datItem1.DatListID = datList1.ID; TodoItem datItem2 = new TodoItem(); datItem2.ID = 40; datItem2.Name = "wash the dishes"; datItem2.IsComplete = true; datItem2.DatListID = datList1.ID; TodoController ic = new TodoController(context); // Act - note: this requires async in order for the // result below to include all of the items in the list // Otherwise, the result query displays as having no // items according to the test even if it actually does. var createdList = await lc.Create(datList1); var createdItem1 = await ic.Create(datItem1); var createdItem2 = await ic.Create(datItem2); var deletedItem = await ic.Delete(datItem1.ID); var result1 = lc.GetById(datList1.ID); var listCount = result1.Result.Value.TodoItems.Count(); var todoInList = result1.Result.Value.TodoItems[0].Name; // Assert Assert.Equal(1, listCount); Assert.Equal(datItem2.Name, todoInList); } }
public async Task Create_Creates_A_TodoItem() { var options = CreateDbContextOptions(); using (var todoApiDbContext = new TodoApiDbContext(options)) { var mockLogger = new Mock <ILogger <TodoController> >(); var todoController = new TodoController(mockLogger.Object, todoApiDbContext); var createdOn = DateTime.UtcNow; var todo = await todoController.Create(new TodoItem() { IsCompleted = false, Description = $"Write test on Create todo item", CreatedOn = createdOn }); Assert.IsType <CreatedAtRouteResult>(todo); var result = todo as CreatedAtRouteResult; Assert.NotNull(result); Assert.IsType <TodoItem>(result.Value); Assert.Equal("GetTodo", result.RouteName); var todoItem = result.Value as TodoItem; Assert.NotEqual(0, todoItem.Id); Assert.Equal(createdOn, todoItem.CreatedOn); Assert.False(todoItem.IsCompleted); } }
async Task CreateTodo() { Cli.DisplayLine("Called CreateTodo()"); Cli.DisplayLine(); var categories = await catctrl.GetAll(); var todo = new Todo(); todo.Id = 0; todo.Description = Cli.Getstring("Enter Description: "); todo.Due = Cli.GetDateTime("Enter valid date: "); todo.Note = Cli.Getstring("Enter Note: "); Cli.DisplayLine("Categories: "); foreach (var c in categories) { Cli.DisplayLine($"{c.Id} : {c.Name}"); } todo.CategoryId = Cli.GetInt("Select category: "); var newTodo = await todoctrl.Create(todo); Cli.DisplayLine(); Cli.DisplayLine("Added . . . "); Cli.DisplayLine(); }
public async Task GetOne_Logging_Working() { var options = CreateDbContextOptions(); using (var todoApiDbContext = new TodoApiDbContext(options)) { var mockLogger = new Mock <ILogger <TodoController> >(); var todoController = new TodoController(mockLogger.Object, todoApiDbContext); var todoCreateResult = await todoController.Create(new TodoItem() { Description = $"Random-{Guid.NewGuid().ToString("N")}" }); var todoTemp = todoCreateResult as CreatedAtRouteResult; var id = (todoTemp.Value as TodoItem).Id; var todo = todoController.GetById(id); Assert.IsType <TodoItem>(todo.Value); mockLogger.Verify(x => x.Log( It.IsAny <LogLevel>(), It.IsAny <EventId>(), It.IsAny <It.IsAnyType>(), It.IsAny <Exception>(), (Func <It.IsAnyType, Exception, string>)It.IsAny <object>())); } }
public void CanCreateTodoItem() { DbContextOptions <TodoDbContext> options = new DbContextOptionsBuilder <TodoDbContext>() .UseInMemoryDatabase("Test1Database").Options; using (TodoDbContext context = new TodoDbContext(options)) { // Arrange TodoItem datItem = new TodoItem(); datItem.ID = 1; datItem.Name = "creating a test method"; datItem.IsComplete = false; TodoController ic = new TodoController(context); // Act var result = ic.Create(datItem); var answer = result.Result; var x = (ObjectResult)answer; // Assert Assert.Equal(HttpStatusCode.Created, (HttpStatusCode)x.StatusCode); } }
public void GivenItemWhenItemIsCreatedThenCreatedOnRouteResultIsReturned() { var item = new TodoItem { Id = 1, Name = "Something" }; var result = sut.Create(item); Assert.IsType <CreatedAtRouteResult>(result); }
public async void CanDeleteTodoItem() { DbContextOptions <TodoDbContext> options = new DbContextOptionsBuilder <TodoDbContext>() .UseInMemoryDatabase("Test4Database").Options; using (TodoDbContext context = new TodoDbContext(options)) { // Arrange // Note: For this test, the ID has to be different // than 1 because async is forcing the ic controller // to create a dummy todo with an ID of 1 TodoItem datItem1 = new TodoItem(); datItem1.ID = 2; datItem1.Name = "walk the dog"; datItem1.IsComplete = false; TodoItem datItem2 = new TodoItem(); datItem2.ID = 3; datItem2.Name = "wash the dishes"; datItem2.IsComplete = true; TodoController ic = new TodoController(context); // Act - note: create and delete need to be async // in order for the result value to display as null // for the deleted playlist var created1 = await ic.Create(datItem1); var created2 = await ic.Create(datItem2); var deletedItem = await ic.Delete(datItem1.ID); var result1 = ic.GetById(datItem1.ID); var result2 = ic.GetById(datItem2.ID); // Assert Assert.Null(result1.Result.Value); Assert.NotNull(result2); } }
public async Task Create_SendBaedRequest_WhenModelInvalid() { //Given var mockRepo = new Mock <ITodoRepository>(); var controller = new TodoController(mockRepo.Object); //When controller.ModelState.AddModelError("Name", "Required"); var result = await controller.Create(new TodoItem()); //Then Assert.IsType <BadRequestObjectResult>(result); }
public void Create_ReturnsBadRequest_GivenNullModel() { // Arrange & Act var mockRepo = new Mock <ITodoRepository>(); var controller = new TodoController(mockRepo.Object); controller.ModelState.AddModelError("error", "some error"); // Act var result = controller.Create(todo: null); // Assert Assert.IsType <BadRequestResult>(result); }
public async Task Create_Returns_Conflict_If_Todo_Exists() { var options = CreateDbContextOptions(); using (var todoApiDbContext = new TodoApiDbContext(options)) { var mockLogger = new Mock <ILogger <TodoController> >(); var todoController = new TodoController(mockLogger.Object, todoApiDbContext); var todo = await todoController.Create(new TodoItem() { Description = "Write Blog Post" }); var todoDuplicate = await todoController.Create(new TodoItem() { Description = "Write Blog Post" }); Assert.IsType <CreatedAtRouteResult>(todo); Assert.IsType <StatusCodeResult>(todoDuplicate); } }
public void TestShouldCreateTodoItem() { // Arrange _todoItems.Add(new TodoItem()); var sut = new TodoController(_todoItems); // Act var result = sut.Create(new TodoItem()); // Assert var actionResult = (CreatedAtRouteResult)result; Assert.AreEqual(201, actionResult.StatusCode); }
public void Create_ReturnsCreatedTodo_GivenCorrectInputs() { // Arrange const string testName = "Test Name"; var newTodo = new Todo { Name = testName }; var mockRepo = new Mock <ITodoRepository>(); var controller = new TodoController(mockRepo.Object); // Act var result = controller.Create(newTodo); // Assert var okResult = Assert.IsType <CreatedAtRouteResult>(result); var returnTodo = Assert.IsType <Todo>(okResult.Value); Assert.Empty(returnTodo.TodoItems); Assert.Equal(testName, returnTodo.Name); }
public async Task Update_Update_An_Existing_Todo() { var options = CreateDbContextOptions(); using (var todoApiDbContext = new TodoApiDbContext(options)) { var mockLogger = new Mock <ILogger <TodoController> >(); var todoController = new TodoController(mockLogger.Object, todoApiDbContext); var todoCreateResult = await todoController.Create(new TodoItem() { Description = "Update this description" }); var todoTemp = todoCreateResult as CreatedAtRouteResult; var id = (todoTemp.Value as TodoItem).Id; var todo = todoController.Update(id, new TodoItem() { Id = id, Description = "Write Blog Post" }); Assert.IsType <NoContentResult>(todo); } }
public void CanUpdateTodoItem() { DbContextOptions <TodoDbContext> options = new DbContextOptionsBuilder <TodoDbContext>() .UseInMemoryDatabase("Test3Database").Options; using (TodoDbContext context = new TodoDbContext(options)) { // Arrange TodoItem datItem1 = new TodoItem(); datItem1.ID = 1; datItem1.Name = "walk the dog"; datItem1.IsComplete = false; datItem1.DatListID = 1; // note: ID must be the same in order to update TodoItem datItem2 = new TodoItem(); datItem2.ID = 1; datItem2.Name = "pet the dog"; datItem2.IsComplete = true; datItem2.DatListID = 2; TodoController ic = new TodoController(context); // Act var created1 = ic.Create(datItem1); var result1 = ic.Update(datItem1.ID, datItem2); var result2 = ic.GetById(datItem1.ID); // Assert Assert.Equal(datItem2.Name, result2.Result.Value.Name); Assert.Equal(datItem2.IsComplete, result2.Result.Value.IsComplete); Assert.Equal(datItem2.DatListID, result2.Result.Value.DatListID); } }
public async Task Create_Logging_Working() { var options = CreateDbContextOptions(); using (var todoApiDbContext = new TodoApiDbContext(options)) { var mockLogger = new Mock <ILogger <TodoController> >(); var todoController = new TodoController(mockLogger.Object, todoApiDbContext); var todo = await todoController.Create(new TodoItem() { Description = "Check logging working on Create" }); Assert.IsType <CreatedAtRouteResult>(todo); mockLogger.Verify(x => x.Log( It.IsAny <LogLevel>(), It.IsAny <EventId>(), It.IsAny <It.IsAnyType>(), It.IsAny <Exception>(), (Func <It.IsAnyType, Exception, string>)It.IsAny <object>()), Times.Exactly(2)); } }
public void Create_ReturnsViewResult_WhenSucceeded() { var result = controller.Create(); var viewResult = Assert.IsType <ViewResult>(result); }