public void Get_All_Success()
        {
            // Act
            var okResult = todotestcotroller.GetAll(new MODELS.PaginationModel.Paging()
            {
                pageNumber = 1, pageSize = 1
            });
            var resultset = (ToDoResponse)((ObjectResult)okResult).Value;

            int resultcount = ((System.Collections.Generic.List <TODO.MODELS.DataModels.ToDoDataModel>)resultset.Data).Capacity;

            // Assert
            Assert.Equal("200", resultset.StatusCode.ToString());
            Assert.Equal(1, resultcount);
        }
Exemple #2
0
        public async Task TestUpdateAccurateData()
        {
            // Arrange
            ToDoController controller    = new ToDoController(new ToDoBL(new ToDoRepository()));
            ToDoController controllerGet = new ToDoController(new ToDoBL(new ToDoRepository()));
            // Act
            var result = await controllerGet.GetAll();

            var data    = result.Data.GetType().GetProperty("data");
            var dataVal = (List <ToDoItem>)data.GetValue(result.Data, null);

            if (dataVal != null && dataVal.Count > 0)
            {
                var item = dataVal[dataVal.Count - 1];
                item.Name = "test update";
                var resultUpdate = await controller.Update(item);

                //Assert
                Assert.IsTrue(resultUpdate.Data != null, "Successfully updated");
            }
            else
            {
                Assert.IsTrue(true);
            }
        }
        public void GetAll_Returns_A_List_Of_ToDos()
        {
            var controller = new ToDoController();
            var result     = controller.GetAll();

            Assert.That(result.Count, Is.EqualTo(1));
        }
        public async void CanGetItems()
        {
            DbContextOptions <ToDoNgDbContext> options = new DbContextOptionsBuilder <ToDoNgDbContext>()
                                                         .UseInMemoryDatabase(Guid.NewGuid().ToString())
                                                         .Options;

            using (ToDoNgDbContext context = new ToDoNgDbContext(options))
            {
                // Arrange
                await context.ToDo.AddRangeAsync(
                    new ToDo()
                {
                    Message = "Hello",
                    IsDone  = true
                },

                    new ToDo()
                {
                    Message = "Goodbye",
                    IsDone  = false
                }
                    );

                await context.SaveChangesAsync();

                ToDoController controller = new ToDoController(context);

                // Act
                OkObjectResult result = controller.GetAll() as OkObjectResult;
                DbSet <ToDo>   items  = result.Value as DbSet <ToDo>;

                // Assert
                Assert.Equal(2, await items.CountAsync());
            }
        }
Exemple #5
0
        public async Task TestUpdateWrongData()
        {
            // Arrange
            ToDoController controller    = new ToDoController(new ToDoBL(new ToDoRepository()));
            ToDoController controllerGet = new ToDoController(new ToDoBL(new ToDoRepository()));
            // Act
            var result = await controllerGet.GetAll();

            var data    = result.Data.GetType().GetProperty("data");
            var dataVal = (List <ToDoItem>)data.GetValue(result.Data, null);

            if (dataVal != null && dataVal.Count > 0)
            {
                var item = dataVal[dataVal.Count - 1];
                item.Name = "";
                controller.ModelState.AddModelError("test", "test");
                var resultUpdate = await controller.Update(item);

                //Assert
                Assert.IsTrue(resultUpdate.Data.ToString().ToLower() == "error", "Validation failed so error occur.");
            }
            else
            {
                Assert.IsTrue(true);
            }
        }
Exemple #6
0
        public async Task TestDeleteAccurateId()
        {
            // Arrange
            ToDoController controller    = new ToDoController(new ToDoBL(new ToDoRepository(), new DataCache()));
            ToDoController controllerGet = new ToDoController(new ToDoBL(new ToDoRepository(), new DataCache()));

            // Act
            var result = await controllerGet.GetAll();

            var data    = result.Data.GetType().GetProperty("data");
            var dataVal = (List <ToDoItem>)data.GetValue(result.Data, null);

            if (dataVal != null && dataVal.Count > 0)
            {
                var item = dataVal[dataVal.Count - 1];


                var resultDelete = await controller.Delete(item.Id);

                //Assert
                Assert.IsTrue(resultDelete.Data.ToString() == "", "Since the id is not present so throwing error.");
            }
            else
            {
                Assert.IsTrue(true);
            }
        }
        public void TestGetAllWithException()
        {
            mock.Expects.One.Method(_ => _.All()).Will(Throw.Exception(new Exception()));
            var controller = new ToDoController(new ToDoBL(mock.MockObject, mockCache.MockObject));
            var result     = controller.GetAll();

            // Assert
            Assert.IsTrue(result.Data.ToString() == "error", "Exception thrown");
        }
Exemple #8
0
        public async Task TestToDoGetAllException()
        {
            // Arrange
            ToDoController controller = new ToDoController(new ToDoMockService());
            //Act
            var result = await controller.GetAll();

            //Assert
            Assert.IsTrue(result.Data.ToString().ToLower() == "error", "Exception occur so error.");
        }
        public void Post_Saves_ToDo()
        {
            var todo       = new ToDo();
            var controller = new ToDoController();

            controller.Post(todo);

            var result = controller.GetAll();

            Assert.That(result.Count, Is.EqualTo(2));
        }
        public async Task GetAll_WithAListOfTodos_ReturnsAList()
        {
            var repoStub = new Mock <IRepository>();

            repoStub.Setup(repo => repo.GetAll <ToDoItem>()).ReturnsAsync(GetTestTodos());
            var controller = new ToDoController(repoStub.Object);
            var res        = await controller.GetAll();

            var listRes = Assert.IsAssignableFrom <List <ToDoItem> >(res.Value);

            Assert.Equal(GetTestTodos().Count, listRes.Count);
        }
 public void TestToDoGetAllException()
 {
     // Arrange
     using (var mock = new ToDoMockService())
     {
         using (ToDoController controller = new ToDoController(mock))
         {
             //Act
             var result = controller.GetAll();
             //Assert
             Assert.IsTrue(result.Data.ToString() == "error", "Exception occur so error.");
         }
     }
 }
        public void TestGetAllWithData()
        {
            var list = new List <DataAccessLayer.Models.ToDoItem>();

            list.Add(new DataAccessLayer.Models.ToDoItem {
                Id = 6, Name = "sampledata"
            });
            mock.Expects.One.Method(_ => _.All()).WillReturn(list.AsQueryable());
            var controller = new ToDoController(new ToDoBL(mock.MockObject, mockCache.MockObject));
            var result     = controller.GetAll();

            // Assert
            Assert.IsNotNull(result);
        }
Exemple #13
0
        public void GetAll_ReturnsAllToDoItems()
        {
            var options = new DbContextOptionsBuilder <ToDoContext>()
                          .UseInMemoryDatabase(databaseName: "ControllerTest")
                          .Options;

            var context    = new ToDoContext(options);
            var controller = new ToDoController(context);

            var items = controller.GetAll();

            Assert.NotEmpty(items);
            Assert.Equal(context.ToDoItems, items);
        }
        public async Task GetAll_ReturnsOkAndToDoList_WhenServiceReturnsToDoList()
        {
            // Arrange

            var mockServiceResponse = new List <BusinessModels.Todo>()
            {
                new BusinessModels.Todo()
                {
                    Id   = Guid.Parse("e0f36c5f-7b2d-45bd-a646-8bb1198879ae"),
                    Name = "ToDo1"
                },
                new  BusinessModels.Todo()
                {
                    Id   = Guid.Parse("a7f01520-537d-47fb-afbf-796607e95701"),
                    Name = "ToDo2"
                }
            };

            _todoServiceMock.Setup(s => s.GetAllTodos()).ReturnsAsync(mockServiceResponse);

            var unitUnderTest = new ToDoController(_mapper, _todoServiceMock.Object);

            var expectedControllerResponse = new List <ApiModels.TodoModel>()
            {
                new ApiModels.TodoModel()
                {
                    Id   = Guid.Parse("e0f36c5f-7b2d-45bd-a646-8bb1198879ae"),
                    Name = "ToDo1"
                },
                new  ApiModels.TodoModel()
                {
                    Id   = Guid.Parse("a7f01520-537d-47fb-afbf-796607e95701"),
                    Name = "ToDo2"
                }
            };

            // Act
            var response = await unitUnderTest.GetAll();

            // Assert
            var okObjectResult = response as OkObjectResult;

            okObjectResult.Should().NotBe(null);
            okObjectResult.StatusCode.Should().Be((int)HttpStatusCode.OK);
            var todoReturned = okObjectResult.Value as List <ApiModels.TodoModel>;

            todoReturned.Should().BeEquivalentTo(expectedControllerResponse);
        }
        public void TestUpdateWrongData()
        {
            // Arrange
            using (var repository = new ToDoRepository())
            {
                using (var businessLogic = new ToDoBL(repository, new DataCache()))
                {
                    using (ToDoController controller = new ToDoController(businessLogic))
                    {
                        using (var repositoryGet = new ToDoRepository())
                        {
                            using (var businessLogicGet = new ToDoBL(repositoryGet, new DataCache()))
                            {
                                using (ToDoController controllerGet = new ToDoController(businessLogicGet))
                                {
                                    // Act
                                    var result  = controllerGet.GetAll();
                                    var data    = result.Data.GetType().GetProperty("data");
                                    var dataVal = (List <ToDoItem>)data.GetValue(result.Data, null);
                                    if (dataVal != null && dataVal.Count > 0)
                                    {
                                        var item = dataVal[dataVal.Count - 1];
                                        item.Name = "";
                                        controller.ModelState.AddModelError("test", "test");
                                        var resultUpdate = controller.Update(item);

                                        //Assert
                                        Assert.IsTrue(resultUpdate.Data.ToString() == "error", "Validation failed so error occur.");
                                    }
                                    else
                                    {
                                        Assert.IsTrue(true);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        public void TestUpdateAccurateData()
        {
            // Arrange
            using (var repository = new ToDoRepository())
            {
                using (var businessLogic = new ToDoBL(repository, new DataCache()))
                {
                    using (ToDoController controller = new ToDoController(businessLogic))
                    {
                        using (var repositoryGet = new ToDoRepository())
                        {
                            using (var businessLogicGet = new ToDoBL(repositoryGet, new DataCache()))
                            {
                                using (ToDoController controllerGet = new ToDoController(businessLogicGet))
                                {
                                    // Act
                                    var result  = controllerGet.GetAll();
                                    var data    = result.Data.GetType().GetProperty("data");
                                    var dataVal = (List <ToDoItem>)data.GetValue(result.Data, null);
                                    if (dataVal != null && dataVal.Count > 0)
                                    {
                                        var item = dataVal[dataVal.Count - 1];
                                        item.Name = "test update";

                                        var resultUpdate = controller.Update(item);

                                        //Assert
                                        Assert.IsTrue(resultUpdate.Data != null, "Successfully updated");
                                    }
                                    else
                                    {
                                        Assert.IsTrue(true);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        public void TestDeleteAccurateId()
        {
            // Arrange
            using (var repository = new ToDoRepository())
            {
                using (var businessLogic = new ToDoBL(repository, new DataCache()))
                {
                    using (ToDoController controller = new ToDoController(businessLogic))
                    {
                        using (var repositoryGet = new ToDoRepository())
                        {
                            using (var businessLogicGet = new ToDoBL(repositoryGet, new DataCache()))
                            {
                                using (ToDoController controllerGet = new ToDoController(businessLogicGet))
                                {
                                    // Act
                                    var result  = controllerGet.GetAll();
                                    var data    = result.Data.GetType().GetProperty("data");
                                    var dataVal = (List <ToDoItem>)data.GetValue(result.Data, null);
                                    if (dataVal != null && dataVal.Count > 0)
                                    {
                                        var item = dataVal[dataVal.Count - 1];

                                        var resultDelete = controller.Delete(item.Id);

                                        //Assert
                                        Assert.IsTrue(String.IsNullOrEmpty(resultDelete.Data.ToString()), "Since the id is not present so throwing error.");
                                    }
                                    else
                                    {
                                        Assert.IsTrue(true);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        public async Task GivenIHave2TodosInDatabase_ControllerSHoulREturn2()
        {
            var controller = new ToDoController(new MockToDoRepository());

            var response = await controller.GetAll();
        }