public async Task get_returns_all_todo() { var result = await _sut.Get(); result.Should().NotBeEmpty(); _mockRepository.Verify(); }
public void Get_OK_Result() { // Act var okResult = todotestcotroller.Get(1); // Assert Assert.IsType <Microsoft.AspNetCore.Mvc.OkObjectResult>(okResult); }
public void DeleteSuccessful() { //Arrange var controller = new ToDoController(new ToDoBL(new ToDoRepository(), new DataCache())); controller.Request = new HttpRequestMessage { Method = HttpMethod.Delete, }; //Actual var toDoItems = controller.Get(); var results = toDoItems.Content.ReadAsAsync <System.Collections.Generic.List <Domain.Models.ToDoItem> >().Result; var controllerDelete = new ToDoController(new ToDoBL(new ToDoRepository(), new DataCache())); controllerDelete.Request = new HttpRequestMessage { Method = HttpMethod.Delete, }; controllerDelete.Configuration = new HttpConfiguration(); var response = controllerDelete.Delete(results[results.Count - 1].Id); //Assert Assert.IsNotNull(response); Assert.IsTrue(response.IsSuccessStatusCode); Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); }
public void GetShouldReturnValidModel() { var mockRepository = new Mock <IToDoItemsRepository>(); var item = new ToDoItem { ItemId = 1, Title = "Item 1", DueDate = DateTime.Now, IsDone = false }; mockRepository.Setup(repo => repo.GetItemById(It.IsAny <int>())).ReturnsAsync(item); var controller = new ToDoController(mockRepository.Object); var result = controller.Get(1).Result as PartialViewResult; Assert.IsNotNull(result.Model); var model = (ToDoItem)result.Model; Assert.AreEqual(item.ItemId, model.ItemId); Assert.AreEqual(item.Title, model.Title); Assert.AreEqual(item.DueDate, model.DueDate); }
public async Task get_by_id_returns_corresponding_value_from_inmemory_repository() { var todo = new Persistence.ToDo { Id = 100, Content = "ToDo 1" }; await _dbContext.Database.EnsureDeletedAsync(); await _dbContext.Database.EnsureCreatedAsync(); _repository = new Repository <Persistence.ToDo>(_dbContext); _sutWithInMemory = new ToDoController(_repository); _sutWithInMemory.ControllerContext = new Microsoft.AspNetCore.Mvc.ControllerContext { HttpContext = new DefaultHttpContext { User = new ClaimsPrincipal(new ClaimsIdentity(new[] { new Claim(ClaimTypes.Name, "Test User"), new Claim(ClaimTypes.NameIdentifier, "*****@*****.**") })) } }; await _sutWithInMemory.Post(todo); var result = await _sutWithInMemory.Get(100); result.Id.Should().Be(100); }
public async Task GetUserToDoListCacheFiveHundredErrorDueToRowVersionBeingNull() { //Arrange var expected = StatusCodes.Status500InternalServerError; var list = new List <ToDoDTO>(); list.Add(new ToDoDTO { Description = "Protect", Name = "Protect Queen of the south", Id = 12154, RowVersion = null }); var mockCache = new Mock <IETagCache>(); mockCache.Setup( cache => cache.GetCachedObject <IEnumerable <ToDoDTO> >($"user-{userId}") ).Returns(list); mockCache.Setup( cache => cache.SetCachedObject(It.IsAny <string>(), It.IsAny <IEnumerable <ToDoDTO> >(), null, It.IsAny <int>()) ).Throws <ArgumentNullException>(); //Act var controller = new ToDoController(new Mock <IToDoService>().Object, _mockLogger, mockCache.Object); var result = await controller.Get(userId, getListDTO) as StatusCodeResult; //Assert Assert.IsNotNull(result); Assert.IsInstanceOf <StatusCodeResult>(result); Assert.AreEqual(expected, result.StatusCode); }
public async Task GetUserToDoListCacheThreeHundredFourResponse() { //Arrange var expected = StatusCodes.Status304NotModified; var list = new List <ToDoDTO>(); list.Add(new ToDoDTO { Description = "Protect", Name = "Protect Queen of the south", Id = 12154 }); var mockCache = new Mock <IETagCache>(); mockCache.Setup( cache => cache.GetCachedObject <IEnumerable <ToDoDTO> >($"user-{userId}") ).Returns(list); //Act var controller = new ToDoController(new Mock <IToDoService>().Object, _mockLogger, mockCache.Object); var result = await controller.Get(userId, getListDTO) as StatusCodeResult; //Assert Assert.IsNotNull(result); Assert.IsInstanceOf <StatusCodeResult>(result); Assert.AreEqual(expected, result.StatusCode); }
public async Task PostSuccessful() { //Arrange var controller = new ToDoController(new ToDoBL(new ToDoRepository())); controller.Request = new HttpRequestMessage { Method = HttpMethod.Post }; var toDoItems = await controller.Get(); var results = toDoItems.Content.ReadAsAsync <System.Collections.Generic.List <Domain.Models.ToDoItem> >().Result; if (results != null && results.Count > 0) { string id = results[results.Count - 1].Id + 1; ToDoItem request = new ToDoItem() { Id = id, Name = "Test Data" }; //Actual var response = await controller.Post(request); //Assert Assert.IsNotNull(response); Assert.IsNotNull(response.Content); Assert.IsTrue(response.IsSuccessStatusCode); Assert.AreEqual(HttpStatusCode.Created, response.StatusCode); } else { Assert.IsTrue(true); } }
public async Task Get_ReturnsOkAndToDo_WhenServiceReturnsToDo() { // Arrange var mockServiceResponse = new BusinessModels.Todo() { Id = Guid.Parse("e0f36c5f-7b2d-45bd-a646-8bb1198879ae"), Name = "ToDo1" }; _todoServiceMock.Setup(s => s.GetTodo(It.IsAny <Guid>())).ReturnsAsync(mockServiceResponse); // We dont care to specify Id, we don't test the logic of the service/repository in controller tests. var unitUnderTest = new ToDoController(_mapper, _todoServiceMock.Object); var expectedControllerResponse = new ApiModels.TodoModel() { Id = Guid.Parse("e0f36c5f-7b2d-45bd-a646-8bb1198879ae"), Name = "ToDo1" }; // Act var response = await unitUnderTest.Get(Guid.NewGuid()); // We dont care to specify Id, we don't test the logic of the service/repository in controller tests. // Assert var okObjectResult = response as OkObjectResult; okObjectResult.Should().NotBe(null); okObjectResult.StatusCode.Should().Be((int)HttpStatusCode.OK); var todoReturned = okObjectResult.Value as ApiModels.TodoModel; todoReturned.Should().BeEquivalentTo(expectedControllerResponse); }
public async Task GetUserToDoTwoHundredResponseCache() { //Arrange var cacheName = $"todo-{toDoId}"; var mockCache = new Mock <IETagCache>(); mockCache.Setup( cache => cache.SetCachedObject(It.IsAny <string>(), It.IsAny <ToDoDTO>(), It.IsAny <byte[]>(), It.IsAny <int>()) ).Returns(true); mockCache.Setup( cache => cache.GetCachedObject <ToDoDTO>(cacheName) ).Returns((ToDoDTO)null); var mockService = new Mock <IToDoService>(); mockService.Setup( service => service.GetToDoById(userId, toDoId) ).ReturnsAsync(_toDoDTO); //Act var controller = new ToDoController(mockService.Object, _mockLogger, mockCache.Object); var result = await controller.Get(userId, toDoId); //Assert Assert.IsNotNull(result); Assert.IsInstanceOf <OkObjectResult>(result); }
public void GetShouldReturnEditPartialView() { var mockRepository = new Mock <IToDoItemsRepository>(); var controller = new ToDoController(mockRepository.Object); var result = controller.Get(5).Result as PartialViewResult; Assert.AreEqual("Edit", result.ViewName); }
public void GetTest() { var controller = new ToDoController { Request = new HttpRequestMessage(), Configuration = new HttpConfiguration() }; IHttpActionResult result = controller.Get(); Assert.IsInstanceOfType(result, typeof(OkNegotiatedContentResult<ToDoEntry[]>)); }
public async Task GetToDoByIdReturnBadRequst() { //Arrange var id = -1; var controller = new ToDoController(_mockService.Object, _mockLogger, _mockCache); //Act var result = await controller.Get(userId, id); //Assert Assert.IsNotNull(id); Assert.IsInstanceOf <BadRequestResult>(result); }
public async Task GetUserListBadRequestUserId() { //Arrange var id = -1; var controller = new ToDoController(new Mock <IToDoService>().Object, _mockLogger, _mockCache); //Act var result = await controller.Get(id, getListDTO); //Assert Assert.IsNotNull(result); Assert.IsInstanceOf <BadRequestResult>(result); }
public void RetrieveToDo_UnitTest_InvalidID() { _toDoRepository.Setup(d => d.GetByID(It.IsAny <int>())).Returns(() => null); var toDoController = new ToDoController(_mockContext.Object); var results = toDoController.Get(5) as NotFoundResult; Assert.IsNotNull(results); Assert.AreEqual(404, results.StatusCode); }
public async Task GetUserToDoByIdReturnNotFound() { //Arrange _mockService.Setup( service => service.GetToDoById(userId, toDoId) ).ReturnsAsync((ToDoDTO)null); var controller = new ToDoController(_mockService.Object, _mockLogger, _mockCache); //Act var result = await controller.Get(userId, toDoId); //Assert Assert.IsNotNull(result); Assert.IsInstanceOf <NotFoundResult>(result); }
public async Task GetUserListsNotFound() { //Arrange _mockService.Setup( service => service.GetToDoByPaging(userId, skip, limit, searchString) ).ReturnsAsync((IEnumerable <ToDoDTO>)(null)); var controller = new ToDoController(_mockService.Object, _mockLogger, _mockCache); //Act var result = await controller.Get(userId, getListDTO); //Assert Assert.IsNotNull(result); Assert.IsInstanceOf <NotFoundResult>(result); }
public void GetShouldReturnErrorForInvalidId() { var mockRepository = new Mock <IToDoItemsRepository>(); var controller = new ToDoController(mockRepository.Object); var actionResult = controller.Get(null).Result; var viewResult = actionResult as PartialViewResult; Assert.IsNull(viewResult); var httpResult = actionResult as HttpStatusCodeResult; Assert.AreEqual((int)HttpStatusCode.BadRequest, httpResult.StatusCode); }
public async Task GetAllException() { //Arrange var controller = new ToDoController(new ToDoMockService()); controller.Request = new HttpRequestMessage { Method = HttpMethod.Get }; //Actual var response = await controller.Get(); //Assert Assert.IsNotNull(response); Assert.IsNull(response.Content); Assert.AreEqual(HttpStatusCode.InternalServerError, response.StatusCode); }
public void GetListRightId() { int listId = 1; var boardMock = new Mock <IStore <Board> >(); var listMock = new Mock <IStore <List> >(); listMock.Setup(store => store.Get(listId)).Returns(() => GetTestLists().FirstOrDefault(list => list.Id == listId)); var eventMock = new Mock <IStore <Event> >(); var userStoreMock = new Mock <IUserStore <ApplicationUser> >(); var userMock = new Mock <UserManager <ApplicationUser> >( userStoreMock.Object, null, null, null, null, null, null, null, null); ToDoController controller = new ToDoController(listMock.Object, userMock.Object, eventMock.Object, boardMock.Object); Assert.Equal(controller.Get(listId).Id, listId); }
public async Task PutSuccessful() { //Arrange var controller = new ToDoController(new ToDoBL(new ToDoRepository())); controller.Request = new HttpRequestMessage { Method = HttpMethod.Put }; controller.Request.Headers.Add("ZUMO-API-VERSION", "2.0.0"); controller.Configuration = new HttpConfiguration(); controller.Configuration.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional }); controller.RequestContext.RouteData = new HttpRouteData( route: new HttpRoute(), values: new HttpRouteValueDictionary { { "controller", "ToDo" } }); var toDoItems = await controller.Get(); var results = toDoItems.Content.ReadAsAsync <System.Collections.Generic.List <Domain.Models.ToDoItem> >().Result; if (results != null && results.Count > 0) { string id = results[results.Count - 1].Id; ToDoItem request = new ToDoItem() { Id = id, Name = "Test Data for Azure " + id }; //Actual var response = await controller.Put(request); //Assert Assert.IsNotNull(response); Assert.IsNotNull(response.Content); Assert.IsTrue(response.IsSuccessStatusCode); Assert.AreEqual(HttpStatusCode.Accepted, response.StatusCode); } else { Assert.IsTrue(true); } }
public async Task GetShouldReturnAllItems() { //Arrange var controller = new ToDoController(new ToDoBL(new ToDoRepository())); controller.Request = new HttpRequestMessage { Method = HttpMethod.Get }; //Actual var response = await controller.Get(); //Assert Assert.IsNotNull(response); Assert.IsNotNull(response.Content); Assert.IsTrue(response.IsSuccessStatusCode); Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); }
public async Task GetUserToDoCacheThreeHundredFourResponse() { //Arrange var expected = StatusCodes.Status304NotModified; var mockCache = new Mock <IETagCache>(); mockCache.Setup( cache => cache.GetCachedObject <ToDoDTO>($"todo-{toDoId}") ).Returns(_toDoDTO); //Act var controller = new ToDoController(new Mock <IToDoService>().Object, _mockLogger, mockCache.Object); var result = await controller.Get(userId, toDoId) as StatusCodeResult; //Assert Assert.IsNotNull(result); Assert.IsInstanceOf <StatusCodeResult>(result); Assert.AreEqual(expected, result.StatusCode); }
public void RetreiveToDos_UnitTest() { _toDoRepository.Setup(d => d.Get(null, null, "")).Returns(_todos); var toDoController = new ToDoController(_mockContext.Object); var actionResult = toDoController.Get(); var okResult = actionResult as OkObjectResult; Assert.IsNotNull(okResult); Assert.AreEqual(200, okResult.StatusCode); var content = okResult.Value as IEnumerable <ToDo>; Assert.AreEqual(4, content.Count()); Assert.AreEqual("Sample Task", content.First().Name); }
public void RetrieveToDo_UnitTest() { _toDoRepository.Setup(d => d.GetByID(It.IsAny <int>())).Returns(_todos.First()); //Run Get Method var toDoController = new ToDoController(_mockContext.Object); var results = toDoController.Get(1); var okResult = results as OkObjectResult; //Assert we got back a good result Assert.IsNotNull(okResult); Assert.AreEqual(200, okResult.StatusCode); //Check the data return to make sure we got the correct item. var content = okResult.Value as ToDo; //////assert Assert.AreEqual(1, content.ToDoId); Assert.AreSame("Sample Task", content.Name); }
public async Task Get_ReturnsNotFound_WhenServiceReturnsNull() { // Arrange _todoServiceMock.Setup(s => s.GetTodo(It.IsAny <Guid>())).ReturnsAsync((BusinessModels.Todo)null); var unitUnderTest = new ToDoController(_mapper, _todoServiceMock.Object); var expectedControllerResponse = new ApiModels.TodoModel() { Id = Guid.Parse("e0f36c5f-7b2d-45bd-a646-8bb1198879ae"), Name = "ToDo1" }; // Act var response = await unitUnderTest.Get(Guid.NewGuid()); // Assert var notFoundResult = response as NotFoundResult; notFoundResult.Should().NotBe(null); notFoundResult.StatusCode.Should().Be((int)HttpStatusCode.NotFound); }
public async Task GetUserListsTwoHundredResponseCache() { //Arrange var cacheName = $"user-{userId}"; var list = new List <ToDoDTO>(); var mockRow = new byte[] { 0, 4 }; list.Add(new ToDoDTO { Description = "Protect", Name = "Protect Queen of the south", Id = 12154, RowVersion = mockRow }); var mockCache = new Mock <IETagCache>(); mockCache.Setup( cache => cache.SetCachedObject(It.IsAny <string>(), It.IsAny <IEnumerable <ToDoDTO> >(), It.IsAny <byte[]>(), It.IsAny <int>()) ).Returns(true); mockCache.Setup( cache => cache.GetCachedObject <IEnumerable <ToDoDTO> >(cacheName) ).Returns((IEnumerable <ToDoDTO>)null); var mockService = new Mock <IToDoService>(); mockService.Setup( service => service.GetToDoByPaging(userId, skip, limit, searchString) ).ReturnsAsync(list); //Act var controller = new ToDoController(mockService.Object, _mockLogger, mockCache.Object); var result = await controller.Get(userId, getListDTO); //Assert Assert.IsNotNull(result); Assert.IsInstanceOf <OkObjectResult>(result); }
public async Task get_returns_all_todo_from_inmemory_repository() { var result = await _sutWithInMemory.Get(); result.Should().NotBeEmpty(); }
public async Task Respond_To_GET() { var result = await _controller.Get(null); Assert.IsInstanceOf(typeof(JArray), result); }