public void setup() { mocks = new MockRepository(); builder = new TestControllerBuilder(); session = mocks.DynamicMock<ISession>(); todoRepository = mocks.StrictMock<TodoRepository>(session); todoController = new TodoController(todoRepository); builder.InitializeController(todoController); }
public void ShouldGetByUserId() { var applicationUserManager = this.GetApplicationUserManagerMock(); var applicationDbContext = this.GetApplicationDbContextMock(); var todoController = new TodoController(applicationUserManager.Object, applicationDbContext.Object); var todos = todoController.GetByUserId().Result as OkNegotiatedContentResult<List<ToDo>>; Assert.IsNotNull(todos); Assert.AreEqual(todos.Content.Count(), 2); }
public async Task GetTodoItem_ReturnsNotFound_WhenTodoItemIsNull() { long testId = 1; var mockRepository = new Mock <ITodoItemRepository>(); mockRepository.Setup(repo => repo.GetItemByIdAsync(testId)) .ReturnsAsync((TodoItem)null); var controller = new TodoController(mockRepository.Object); var result = await controller.GetTodoItem(testId); Assert.IsType <NotFoundResult>(result.Result); }
public void TestGetAll() { var ctrl = new TodoController(_context, _logger); var items = ctrl.GetAll(); var iter = items.GetEnumerator(); iter.MoveNext(); Assert.Equal(iter.Current.Name, "Item1"); iter.MoveNext(); Assert.Equal(iter.Current.Name, "Item2"); }
public void ShouldGetOne() { var applicationUserManager = this.GetApplicationUserManagerMock(); var applicationDbContext = this.GetApplicationDbContextMock(); var todoController = new TodoController(applicationUserManager.Object, applicationDbContext.Object); var todo = todoController.GetOne(1).Result as OkNegotiatedContentResult<ToDo>; Assert.IsNotNull(todo); Assert.AreEqual(todo.Content.Id, 1); Assert.AreEqual(todo.Content.User.Id, "1"); }
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 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 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 Delete_Returns_NoContent_If_Success() { 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.Delete(1); Assert.IsType <NoContentResult>(todo); } }
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 Can_404_When_Getting_A_Missing_Todo() { var fixture = new TodoDataFixture(); var subject = new TodoController(fixture.Repository); var result = (await subject.Get("BADID")).Result as NotFoundObjectResult; Assert.NotNull(result); Assert.Equal(404, result.StatusCode); Assert.IsAssignableFrom <ApiErrorModel>(result.Value); Assert.Equal( "not_found", ((ApiErrorModel)result.Value).Code ); }
public void DeleteTodo_Invalid_Request() { // Arrange todoController = new TodoController(todoMockDal); todoController.Request = new HttpRequestMessage(); todoController.Request.SetConfiguration(new HttpConfiguration()); //Act var result = todoController.DeleteTodo(0); //Assert //Assert.IsInstanceOf<BadRequestResult>(result); Assert.AreEqual(HttpStatusCode.BadRequest, result.StatusCode); }
public void GetAllTodo_Positive() { // Arrange todoController = new TodoController(todoMockDal); todoController.Request = new HttpRequestMessage(); todoController.Request.SetConfiguration(new HttpConfiguration()); //Act var result = todoController.GetAllTodo(); //Assert Assert.AreEqual(HttpStatusCode.OK, result.StatusCode); //Assert.AreEqual(2, result.Content.); }
public async Task GetAll_Returns_ListOfTodos() { var options = CreateDbContextOptions(); using (var todoApiDbContext = new TodoApiDbContext(options)) { var mockLogger = new Mock <ILogger <TodoController> >(); var todoController = new TodoController(mockLogger.Object, todoApiDbContext); var listOfTodosResult = await todoController.GetAll(); Assert.IsType <List <TodoItem> >(listOfTodosResult.Value); } }
public void DeleteItem() { var controller = new TodoController(this.unitOfWork); var all = controller.GetAll(); var initialCount = all.Count(); var deleteMe = all.FirstOrDefault(); controller.DeleteItem(deleteMe.Id); var finalCount = controller.GetAll().Count(); Assert.Equal(1, initialCount - finalCount); }
public async void Update_SingleTodo_ValidId_ValidBody_ShouldSucceed() { var _dbContext = new TodoContext(new DbContextOptionsBuilder <TodoContext>().UseInMemoryDatabase("todo-db4").Options); TodoController controller = new TodoController(_dbContext); await controller.AddTodo(new TodoItem { Description = "Laundry", Date = DateTime.UtcNow }); var item = _dbContext.TodoItems.First(); var result = await controller.UpdateTodo(item.Id, new TodoItem { Id = item.Id, Description = "Laundry 2", Date = item.Date, Completed = true }); Assert.IsType <OkResult>(result); item = _dbContext.TodoItems.First(); Assert.Equal("Laundry 2", item.Description); Assert.True(item.Completed); }
public async Task Can_Get_All_Todos() { var fixture = new TodoDataFixture(); var subject = new TodoController(fixture.Repository); var result = (await subject.Get()).Result as OkObjectResult; Assert.NotNull(result); Assert.Equal(200, result.StatusCode); Assert.Equal( new[] { TodoDataFixture.TodoApiModel }, ((IEnumerable <TodoApiModel>)result.Value).ToArray(), new TodoApiModelComparer() ); }
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 Can_Get_A_Todo() { var fixture = new TodoDataFixture(); var subject = new TodoController(fixture.Repository); var result = (await subject.Get("test-id")).Result as OkObjectResult; Assert.NotNull(result); Assert.Equal(200, result.StatusCode); Assert.Equal( TodoDataFixture.TodoApiModel, (TodoApiModel)result.Value, new TodoApiModelComparer() ); }
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 async Task AddItemGet_ShouldReturn_ChallengeResult_IfUserIsUnauthorize() { // Arrange // Make the mockUserManager always return null for GetUserAsync mockUserManager .Setup(x => x.GetUserAsync(It.IsAny <ClaimsPrincipal>())) .Returns(Task.FromResult <IdentityUser>(null)); var controller = new TodoController(mockTodoItemService.Object, mockCategoryService.Object, mockUserManager.Object); // Act var result = await controller.AddItem(); // Assert Assert.IsType <ChallengeResult>(result); }
public void GetByTitle_Negative_ReturnsNotFound() { var datarepo = new Mock <IDataRepo>(); List <Note> notes = GetMockDatabase(); string type = Constant.Type.Title; string text = "NonExistentTitle"; datarepo.Setup(d => d.GetNote(text, type)).Returns(notes.FindAll(n => n.Title == text)); TodoController todoController = new TodoController(datarepo.Object); var actionResult = todoController.Get(text, type); var nfObjectResult = actionResult as NotFoundObjectResult; Assert.NotNull(nfObjectResult); }
public void DeleteShouldReturnOk() { var mockCurrentUser = new Mock <ICurrentUser>(); var mockTodoRepository = new Mock <ITodoRepository>(); mockTodoRepository .Setup(_ => _.GetById(It.IsAny <int>())) .Returns(new Todo()); var controller = new TodoController(mockCurrentUser.Object, mockTodoRepository.Object); var response = controller.Delete(1); Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); }
public TodoControllerTest() { var optionsBuilder = new DbContextOptionsBuilder <TodoContext>(); optionsBuilder.UseInMemoryDatabase("test"); todoContext = new TodoContext(optionsBuilder.Options); todoContext.Todos.Add(new Todo { Id = 1, Title = "Go to the supermarket" }); todoContext.SaveChanges(); controller = new TodoController(todoContext); }
public async Task InvalidTodoItem_ShouldReturn_NotFound() { //Arrange var mockRepository = new Mock <ITodoRepository>(); //Establish that our repo doesn't contain any items with ID = -1 mockRepository.Setup(repo => repo.GetByID(-1)).Returns(null as TodoItem); var controller = new TodoController(mockRepository.Object); //Act var result = await controller.Get(-1); //Assert var okResult = result.Should().BeOfType <NotFoundResult>().Subject; }
public async Task GetReturnListOfTodo() { var mockRepo = new Mock <ITodoRepository>(); mockRepo.Setup(repo => repo.GetAll()) .Returns(Task.FromResult(GetTestTodoList())); var controller = new TodoController(mockRepo.Object); var result = await controller.GetAll(); var okResult = Assert.IsType <OkObjectResult>(result); var returnedResult = Assert.IsType <List <TodoItem> >(okResult.Value); Assert.Equal(1, returnedResult.Count()); }
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 GetByTitle_Negative_ReturnsBadRequest() { var datarepo = new Mock <IDataRepo>(); List <Note> notes = null; string type = "NonExistentType"; string text = "NonExistentTitle"; datarepo.Setup(d => d.GetNote(text, type)).Returns(notes); TodoController todoController = new TodoController(datarepo.Object); var actionResult = todoController.Get(text, type); var brObjectResult = actionResult as BadRequestObjectResult; Assert.NotNull(brObjectResult); }
public void TestDeadLineDateFunction_SucessAfterDeadline() { TodoController controller = new TodoController(); Todo todo = new Todo(); todo.ID = 1; todo.deadLineDate = DateTime.Now.Date.AddDays(10); todo.isComplete = false; todo.moreDetails = "More Details"; todo.toDo_Item = "Wash Face"; var returnVal = controller.isPassedDeadline(todo.deadLineDate); Assert.IsTrue(returnVal); }
public void GetAllTodosReturnsFullList() { var testList = new List <EF.Entities.Todo>() { }; var mockTodoService = new Mock <ITodoService>(); mockTodoService.Setup(s => s.GetAll()).Returns(testList); var fakeTodoService = mockTodoService.Object; todoController = new TodoController(fakeTodoService); var actionResult = todoController.List(); }
public void GetAll_ShouldReturnOkHttpStatusCodeWithItems_InitializeCollection() { // Arrange var mockRepo = new Mock <ITodoRepository>(); mockRepo.Setup(repo => repo.GetAll()) .Returns(GetAllTodoItems()); var controller = new TodoController(mockRepo.Object); // Act var result = controller.GetAll(); // Assert Assert.IsType <ActionResult <IEnumerable <TodoItem> > >(result); }
public async Task GetTodoItems() { // Arrange var mockRepo = new Mock <ITodoRepository>(); mockRepo.Setup(repo => repo.SelectAllAsync()).ReturnsAsync(GetTestTodoList); var controller = new TodoController(mockRepo.Object); // Act var result = await controller.GetTodoItems(); // Assert Assert.Equal(2, result.Value.Count()); Assert.Equal("Item1", result.Value.First().Name); Assert.Equal("Item2", result.Value.Last().Name); }
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 setup() { builder = new TestControllerBuilder(); todoController = new TodoController(); builder.InitializeController(todoController); }