Example #1
0
        public void TodoController_GetAll_return_Internal_Error()
        {
            _tasksService.Setup(x => x.GetAll()).Throws(new Exception());

            var response = _todoController.Get();

            Assert.AreEqual(HttpStatusCode.InternalServerError, response.StatusCode);
        }
Example #2
0
        public void Get_WhenCalled_ReturnsOkResult()
        {
            // Act
            var serviceResult = _controller.Get().Result;
            var okResult      = serviceResult;

            // Assert
            Assert.IsType <OkObjectResult>(okResult.Result);
        }
Example #3
0
        public async Task GivenATodoController_WhenGet_ThenAListOfTodosIsReturned()
        {
            _mockService.Setup(service => service.GetAll()).Returns(Task.FromResult <IEnumerable <TodoDto> >(new List <TodoDto> {
                new TodoDto()
            }));
            var result = await _sut.Get();

            var okObjectResult = Assert.IsType <OkObjectResult>(result);

            Assert.Equal(200, okObjectResult.StatusCode);
            var data = (IEnumerable <TodoDto>)okObjectResult.Value;

            Assert.Equal(1, data.Count());
        }
Example #4
0
        public async Task Should_OkstatusCode_WhenGetTodos()
        {
            var todos = await GetTestTodos();

            var testTodoParams = GetTestTodoParams(1, 25, "", false);

            _mockRepo.Setup(repo => repo.GetTodos(testTodoParams)).ReturnsAsync(todos);
            _mockRepo.Setup(repo => repo.GetTodos(testTodoParams)).ReturnsAsync(todos);

            var httpContext       = new DefaultHttpContext();
            var controllerContext = new ControllerContext()
            {
                HttpContext = httpContext,
            };

            var controller = new TodoController(_mockRepo.Object, _mockMapper.Object)
            {
                ControllerContext = controllerContext,
            };

            var result = await controller.Get(testTodoParams);

            var okResult = result as OkObjectResult;

            Assert.IsNotNull(okResult);
            Assert.AreEqual(200, okResult.StatusCode);
        }
        public async Task ValidTodoItem_ShouldReturn_OkResponse()
        {
            //Arrange
            var mockRepository = new Mock <ITodoRepository>();

            //Establish a response for an item of ID = 1
            mockRepository.Setup(repo => repo.GetByID(1)).Returns(new TodoItem {
                ItemId      = 1,
                Title       = "Blah",
                Description = "Hmmm",
                Completed   = true
            });
            var controller = new TodoController(mockRepository.Object);

            //Act
            var result = await controller.Get(1);

            //Assert
            var okResult = result.Should().BeOfType <OkObjectResult>().Subject;

            (okResult.Value as TodoItem).Title.Should().BeEquivalentTo("Blah");
            (okResult.Value as TodoItem).Description.Should().BeEquivalentTo("Hmmm");
            (okResult.Value as TodoItem).ItemId.Should().Equals(1);
            (okResult.Value as TodoItem).Completed.Should().Be(true);
        }
Example #6
0
        public async void Get_AllTodoItems()
        {
            // Arrange
            var entities = new List <TodoItem>
            {
                new TodoItem {
                    Id = 1, Name = "Test 1"
                },
                new TodoItem {
                    Id = 2, Name = "Test 2"
                },
                new TodoItem {
                    Id = 3, Name = "Test 3"
                }
            };
            var context   = new Mock <BasicArchitectureTemplateDbContext>();
            var dbSetMock = ServiceTestsHelper.GetMockDbSet <TodoItem>(entities);

            context.Setup(x => x.Set <TodoItem>()).Returns(dbSetMock.Object);
            var repositoryMock = new Mock <IRepository <TodoItem, int> >();

            repositoryMock.Setup(x => x.Query()).Returns((IQueryable <TodoItem>)dbSetMock.Object);
            var service = new TodoService(repositoryMock.Object);

            var controller = new TodoController(new NullLogger <TodoController>(), service);
            // Act
            var result = await controller.Get();

            //Assert
            var model = Assert.IsType <List <TodoItem> >(result);

            Assert.Equal(3, model.Count());
        }
Example #7
0
        public void Test_Get_Success()
        {
            const int id   = 1;
            var       todo = new TodoItem()
            {
                Id = id
            };

            _todosRepository.Setup(x => x.Get(id))
            .Returns(todo);

            var result      = _todoController.Get(id);
            var todoFromGet = (result.Result as OkObjectResult)?.Value as TodoItem;

            Assert.IsInstanceOfType(result.Result, typeof(OkObjectResult));
            Assert.IsTrue(todo.Equals(todoFromGet));
        }
Example #8
0
        public void Get_Returns_List_of_Todos()
        {
            var underTest = new TodoController();

            var result = underTest.Get();

            Assert.Equal(3, result.Value.Count());
        }
        public void GetAll_Negative_DatabaseError()
        {
            var         datarepo = new Mock <IDataRepo>();
            List <Note> notes    = null;

            datarepo.Setup(d => d.GetAllNotes()).Returns(notes);
            TodoController todoController = new TodoController(datarepo.Object);
            var            result         = todoController.Get();

            Assert.IsType <NotFoundObjectResult>(result);
        }
Example #10
0
        public void CanGetAllTodos()
        {
            //Arange
            var controller = new TodoController();

            //Act
            var todos = controller.Get();

            //Assert
            Assert.NotNull(todos);
            Assert.NotEmpty(todos);
        }
        public void GetAll_Positive()
        {
            var         mockdata = new Mock <IDbRepo>();
            List <Note> notes    = GetMockDatabase();

            mockdata.Setup(d => d.GetAllNotes()).Returns(notes);
            TodoController notescontroller = new TodoController(mockdata.Object);
            var            result          = notescontroller.Get();

            Assert.NotNull(result);
            Assert.Equal(2, notes.Count);
        }
Example #12
0
        public async Task Should_OkstatusCode_WhenGetTodo()
        {
            _mockRepo.Setup(repo => repo.GetTodo("4bbcfda8-b2bc-4e46-a174-f4c8cc001596")).ReturnsAsync(GetTestTodo());

            var controller = new TodoController(_mockRepo.Object, _mockMapper.Object);
            var result     = await controller.Get("4bbcfda8-b2bc-4e46-a174-f4c8cc001596");

            var okResult = result as OkObjectResult;

            Assert.IsNotNull(okResult);
            Assert.AreEqual(200, okResult.StatusCode);
        }
        public void GetById_Negative_ReturnsNullNotFound()
        {
            var         datarepo = new Mock <IDataRepo>();
            List <Note> notes    = GetMockDatabase();
            int         id       = 3;

            datarepo.Setup(d => d.GetNote(id)).Returns(notes.Find(n => n.NoteId == id));
            TodoController todoController = new TodoController(datarepo.Object);
            var            result         = todoController.Get(id);

            Assert.IsType <NotFoundObjectResult>(result);
        }
        public void RetrieveNote_Id1_Positive()
        {
            var         noterepo = new Mock <INoteRepository>();
            List <Note> notes    = GetMockDatabase();
            int         id       = 1;

            noterepo.Setup(d => d.RetrieveNote(id)).Returns(notes.Find(n => n.NoteId == id));
            TodoController todoController = new TodoController(noterepo.Object);
            var            result         = todoController.Get(id);

            Assert.NotNull(result);
            Assert.Equal(id, result.Value.NoteId);
        }
Example #15
0
        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()
                );
        }
Example #16
0
        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()
                );
        }
Example #17
0
        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
                );
        }
Example #18
0
        public void Get_Returns_List_of_Todos()
        {
            var expectedTodos = new List <Todo>()
            {
                new Todo(1, "First item"),
                new Todo(2, "Second item")
            };

            todoRepo.GetAll().Returns(expectedTodos);

            var result = underTest.Get();

            Assert.Equal(expectedTodos, result.ToList());
        }
        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 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 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 void TodoControllerTest_GetTodoList()
        {
            var mockTodoService = new Mock <ITodoService>();

            var todoListDTO = new List <TodoGetDTO>();

            mockTodoService.Setup(x => x.FindAll())
            .Returns(todoListDTO);

            TodoController todoController = new TodoController(mockTodoService.Object);

            // Act
            List <TodoGetDTO> actionResult = todoController.Get();

            // Assert
            Assert.AreEqual(todoListDTO, actionResult);
        }
Example #23
0
        public void UserFailTest()
        {
            // Arrange
            var mockServ   = new Mock <ITodoService>();
            var controller = new TodoController(mockServ.Object);

            // Act
            var actualGetResp    = controller.Get(null) as BadRequestResult;
            var actualPostResp   = controller.Post(null) as BadRequestResult;
            var actualPutResp    = controller.Put(0, null) as BadRequestResult;
            var actualDeleteResp = controller.Delete(0) as BadRequestResult;

            // Assert
            Assert.IsNotNull(actualGetResp);
            Assert.IsNotNull(actualPostResp);
            Assert.IsNotNull(actualPutResp);
            Assert.IsNotNull(actualDeleteResp);
        }
        public void GetShouldReturnEmptyResult()
        {
            var mockCurrentUser = new Mock <ICurrentUser>();

            mockCurrentUser.Setup(_ => _.UserId).Returns("USERID");

            var mockTodoRepository = new Mock <ITodoRepository>();

            mockTodoRepository.Setup(_ => _.GetAll())
            .Returns(new List <Todo> {
                new Todo()
            });

            var controller = new TodoController(mockCurrentUser.Object, mockTodoRepository.Object);

            var result = controller.Get();

            Assert.AreEqual(0, result.Count());
        }
        public void GetAll_Negative_EmptyList()
        {
            var         datarepo = new Mock <IDataRepo>();
            List <Note> notes    = new List <Note>();

            datarepo.Setup(d => d.GetAllNotes()).Returns(notes);
            TodoController todoController = new TodoController(datarepo.Object);
            var            actionResult   = todoController.Get();

            var okObjectResult = actionResult as OkObjectResult;

            Assert.NotNull(okObjectResult);

            var model = okObjectResult.Value as List <Note>;

            Assert.NotNull(model);

            Assert.Equal(notes.Count, model.Count);
        }
Example #26
0
        public void RetrieveById_positive()
        {
            var         datarepo = new Mock <IDataRepo>();
            List <Note> notes    = GetMockDatabase();
            int         id       = 1;

            datarepo.Setup(d => d.RetrieveById(id)).Returns(notes.Find(n => n.NoteId == id));
            TodoController todoController = new TodoController(datarepo.Object);
            var            result         = todoController.Get(id);

            Assert.NotNull(result);
            var objResult = result as OkObjectResult;

            Assert.NotNull(objResult);
            var note = objResult.Value as Note;

            Assert.NotNull(note);
            Assert.Equal(id, note.NoteId);
        }
Example #27
0
        public void GetAllTest()
        {
            var userId = 1;

            // Arrange
            var mockServ = new Mock <ITodoService>();

            mockServ.Setup(s => s.GetAllTodoByUser(userId)).Returns(GetAllTodo);
            var controller = new TodoController(mockServ.Object);

            controller.ControllerContext = FakeContext;

            // Act
            var actualResp = controller.Get(null) as OkObjectResult;
            var actual     = actualResp.Value as List <Todo>;

            // Assert
            Assert.AreEqual(actual[0].Id, GetAllTodo[0].Id);
            Assert.AreEqual(actual[1].Id, GetAllTodo[1].Id);
        }
        public void GetById_Positive_ReturnsNoteWithId1()
        {
            var         datarepo = new Mock <IDataRepo>();
            List <Note> notes    = GetMockDatabase();
            int         id       = 1;

            datarepo.Setup(d => d.GetNote(id)).Returns(notes.Find(n => n.NoteId == id));
            TodoController todoController = new TodoController(datarepo.Object);
            var            actionResult   = todoController.Get(id);

            var okObjectResult = actionResult as OkObjectResult;

            Assert.NotNull(okObjectResult);

            var model = okObjectResult.Value as Note;

            Assert.NotNull(model);

            Assert.Equal(id, model.NoteId);
        }
Example #29
0
        public void GetSingleTest()
        {
            var userId = 1;
            var todoId = 1;
            var todo   = GetAllTodo.First(t => t.Id == todoId && t.UserId == userId);

            // Arrange
            var mockServ = new Mock <ITodoService>();

            mockServ.Setup(s => s.GetTodo(todoId, userId)).Returns(todo);
            var controller = new TodoController(mockServ.Object);

            controller.ControllerContext = FakeContext;

            // Act
            var actualResp = controller.Get(1) as OkObjectResult;
            var actual     = actualResp.Value as Todo;

            // Assert
            Assert.AreEqual(actual.Id, todo.Id);
        }
Example #30
0
        public async Task GetTodoItemSuccess()
        {
            // Arrange
            var mockTodoItems = new Mock <DbSet <TodoItem> >();

            mockTodoItems.As <IAsyncEnumerable <TodoItem> >().Setup(m => m.GetEnumerator()).Returns(new MockAsyncEnumerator <TodoItem>(_todoItems.GetEnumerator()));
            mockTodoItems.As <IQueryable <TodoItem> >().Setup(m => m.Provider).Returns(new MockAsyncQueryProvider <TodoItem>(_todoItems.Provider));
            mockTodoItems.As <IQueryable <TodoItem> >().Setup(m => m.Expression).Returns(_todoItems.Expression);
            mockTodoItems.As <IQueryable <TodoItem> >().Setup(m => m.ElementType).Returns(_todoItems.ElementType);
            mockTodoItems.As <IQueryable <TodoItem> >().Setup(m => m.GetEnumerator()).Returns(_todoItems.GetEnumerator());

            var mockTodoContext = new Mock <TodoContext>();

            mockTodoContext.Setup(todoContext => todoContext.TodoItems).Returns(mockTodoItems.Object);
            var controller = new TodoController(mockTodoContext.Object);

            // Act
            var result = await controller.Get(1050);

            // Assert
            Assert.IsType <ActionResult <TodoItem> >(result);
        }