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 void GivenIdWhenIdIsFoundThenItemIsReturned() { long id = 5; var item = new TodoItem { Id = 1, Name = "Something" }; repoMock.Setup(x => x.GetById(id)).Returns(item); var actual = sut.GetById(id); Assert.Equal(1, actual.Value.Id); Assert.Equal("Something", actual.Value.Name); }
public void Test_GetById_NonExisitngItem() { var controller = new TodoController(_context); var result = controller.GetById(2); Assert.IsType <NotFoundResult>(result); }
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 GetTodoItemById(int id) { var controller = new TodoController(this.unitOfWork); var result = controller.GetById(id) as ObjectResult; var todoItem = result.Value as TodoItem; Assert.Equal(id, todoItem.Id); }
public void Test_GetById_ExistingItem() { var controller = new TodoController(_context); var result = controller.GetById(1); var resultType = Assert.IsType <ObjectResult>(result); var item = (TodoItem)resultType.Value; Assert.Equal(1, item.Id); }
public async Task Get_Item_By_Id() { var id = 1; var result = await ControllerUnderTest.GetById(id) as OkObjectResult; var item = result.Value as TodoItem; Assert.AreEqual(item.Id, 1); Assert.AreEqual(item.Name, "None"); }
public async Task TodoGetById_WithValidModel_Return_OkResult() { //Arrange var id = 1; //Act var result = await _controller.GetById(id); //Assert Assert.IsType <OkObjectResult>(result); }
public void TestGeTaskById_NotFound() { // Arrange var controller = new TodoController(_todoContext); // Act var response = controller.GetById(0); // Assert Assert.IsType(typeof(NotFoundResult), response); }
public void TestGeTaskById(int id) { // Arrange var controller = new TodoController(_todoContext); // Act var response = controller.GetById(id); // Assert Assert.IsType(typeof(ObjectResult), response); }
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 void GetByIdShouldSendNotFoundWhenTodoItemNotFound() { var testId = 2; var mockRepo = new Mock <ITodoRepository>(); mockRepo.Setup(repo => repo.Find(testId)) .Returns(Task.FromResult((TodoItem)null)); var controller = new TodoController(mockRepo.Object); var result = await controller.GetById(testId); Assert.IsType <NotFoundResult>(result); }
public void GetOne_Returns_OneTodo() { var options = CreateDbContextOptions(); using (var todoApiDbContext = new TodoApiDbContext(options)) { var mockLogger = new Mock <ILogger <TodoController> >(); var todoController = new TodoController(mockLogger.Object, todoApiDbContext); var todo = todoController.GetById(1); Assert.IsType <TodoItem>(todo.Value); } }
public void GetOne_Returns_NotFound() { var options = CreateDbContextOptions(); using (var todoApiDbContext = new TodoApiDbContext(options)) { var mockLogger = new Mock <ILogger <TodoController> >(); var todoController = new TodoController(mockLogger.Object, todoApiDbContext); var todo = todoController.GetById(11355); Assert.IsType <NotFoundResult>(todo.Result); } }
public void TestGetById() { var ctrl = new TodoController(_context, _logger); var res = ctrl.GetById(1) as ObjectResult; Assert.NotNull(res); var item = res.Value as TodoItem; Assert.NotNull(item); Assert.Equal(item.Name, "Item1"); }
public void Test_Delete_SuccessCase() { var item = new TodoItem { Name = "Item to test delete" }; var controller = new TodoController(_context); item = (TodoItem)((CreatedAtRouteResult)controller.CreateItem(item)).Value; var deleteResult = controller.Delete(item.Id); Assert.IsType <NoContentResult>(deleteResult); var getResult = controller.GetById(item.Id); Assert.IsType <NotFoundResult>(getResult); }
public void GetOne_Logging_Working_When_NotFound() { var options = CreateDbContextOptions(); using (var todoApiDbContext = new TodoApiDbContext(options)) { var mockLogger = new Mock <ILogger <TodoController> >(); var todoController = new TodoController(mockLogger.Object, todoApiDbContext); var todo = todoController.GetById(11355); Assert.IsType <NotFoundResult>(todo.Result); 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 CreateTodoItem(int id) { var controller = new TodoController(this.unitOfWork); var context = new DefaultHttpContext(); controller.ActionContext.HttpContext = context; var createMe = new TodoItem { Id = id }; controller.CreateTodoItem(createMe); var created = controller.GetById(id) as ObjectResult; Assert.Equal(id, (created.Value as TodoItem).Id); var statusCode = context.Response.StatusCode; Assert.Equal(201, statusCode); }
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 void GetById_InvalidGuidPassed_ReturnsNotFoundResult() { var notFoundResult = _controller.GetById(Guid.NewGuid()); Assert.IsType <NotFoundResult>(notFoundResult.Result.Result); }
public void ShouldReturnSpecificTodoItem() { Assert.That(new TodoItem { Id = 1, Name = "Mow the Lawn", IsComplete = false }, Is.EqualTo(controller.GetById(1L).Value)); }
public void ValuesController_GetId_Returns_NotNull() { string res = _todoController.GetById("Nyckel").ToString(); Assert.NotNull(res); }