public void DeleteBreedOkTest()
        {
            //Arrange

            Guid fakeGuid = Guid.NewGuid();

            var mockBreedsBusinessLogic = new Mock <IBreedsBusinessLogic>();

            mockBreedsBusinessLogic
            .Setup(bl => bl.Delete(It.IsAny <Guid>()))
            .Returns(It.IsAny <bool>());

            var controller = new BreedsController(mockBreedsBusinessLogic.Object);

            // Configuramos la Request (dado que estamos utilziando HttpResponseMessage)
            // Y usando el objeto Request adentro.
            ConfigureHttpRequest(controller);

            //Act
            HttpResponseMessage obtainedResult = controller.Delete(fakeGuid);

            //Assert
            mockBreedsBusinessLogic.VerifyAll();
            Assert.IsNotNull(obtainedResult);
        }
        public void UpdateExistingBreedOkTest()
        {
            //Arrange
            var fakeBreed      = GetAFakeBreed();
            var expectedResult = true;

            var mockBreedsBusinessLogic = new Mock <IBreedsBusinessLogic>();

            mockBreedsBusinessLogic
            .Setup(bl => bl.Update(It.IsAny <Guid>(), It.IsAny <Breed>()))
            .Returns(true);

            var controller = new BreedsController(mockBreedsBusinessLogic.Object);

            //Act
            IHttpActionResult obtainedResult = controller.Put(new Guid(), fakeBreed);
            var createdResult = obtainedResult as CreatedAtRouteNegotiatedContentResult <Breed>;

            //Assert
            mockBreedsBusinessLogic.VerifyAll();
            Assert.IsNotNull(createdResult);
            Assert.AreEqual("DefaultApi", createdResult.RouteName);
            Assert.AreEqual(expectedResult, createdResult.RouteValues["updated"]);
            Assert.AreEqual(fakeBreed, createdResult.Content);
        }
 private void ConfigureHttpRequest(BreedsController controller)
 {
     controller.Request       = new HttpRequestMessage();
     controller.Configuration = new HttpConfiguration();
     controller.Configuration.Routes.MapHttpRoute(
         name: "DefaultApi",
         routeTemplate: "api/{controller}/{id}",
         defaults: new { id = RouteParameter.Optional });
 }
Пример #4
0
        public async void GetDeleteNotFound()
        {
            using (var database = new TestDb())
            {
                var controller = new BreedsController(database.Context);

                IActionResult result = await controller.Delete(50);

                CheckMessage(result, "The selected Breed could not be found.");
            }
        }
Пример #5
0
        public async void PostCreateParentBreedDoesntExist()
        {
            using (var database = new TestDb())
            {
                var controller = new BreedsController(database.Context);

                IActionResult result = await controller.Create(new Breed { Name = "Test Breed with Invalid Parent Breed", BreedId = 500 });

                CheckMessage(result, "The parent Breed selected could not be found.");
            }
        }
Пример #6
0
        public async void PostEditNotFound()
        {
            using (var database = new TestDb())
            {
                var controller = new BreedsController(database.Context);

                IActionResult result = await controller.Edit(50, new Breed(50, "Test Breed 6 Modified"));

                CheckMessage(result, "The selected Breed could not be found.");
            }
        }
Пример #7
0
        public async void PostEditParentSetAsOwnParent()
        {
            using (var database = new TestDb())
            {
                var controller = new BreedsController(database.Context);

                IActionResult result = await controller.Edit(1, new Breed { Id = 1, Name = "Test Breed with Null Parent Breed", BreedId = 1 });

                CheckMessage(result, "A breed cannot be set as it's own Parent Breed.");
            }
        }
Пример #8
0
        public async void PostEditMismatchedIds()
        {
            using (var database = new TestDb())
            {
                var controller = new BreedsController(database.Context);

                IActionResult result = await controller.Edit(7, new Breed(6));

                CheckMessage(result, "The Breed ID provided doesn't match the Breed being updated.");
            }
        }
Пример #9
0
        public async void PostEditNegativeId()
        {
            using (var database = new TestDb())
            {
                var controller = new BreedsController(database.Context);

                IActionResult result = await controller.Edit(-1, new Breed(-1, string.Empty));

                CheckMessage(result, $"The Breed ID must be between 1 and {int.MaxValue}.");
            }
        }
Пример #10
0
        public async void PostEditNoName()
        {
            using (var database = new TestDb())
            {
                var controller = new BreedsController(database.Context);

                IActionResult result = await controller.Edit(1, new Breed(1, string.Empty));

                CheckMessage(result, "A Breed must have a name.");
            }
        }
Пример #11
0
        public async void PostCreateNoName()
        {
            using (var database = new TestDb())
            {
                var controller = new BreedsController(database.Context);

                IActionResult result = await controller.Create(new Breed());

                CheckMessage(result, "A Breed must have a name.");
            }
        }
Пример #12
0
        public async void PostEditParentBreedNegative()
        {
            using (var database = new TestDb())
            {
                var controller = new BreedsController(database.Context);

                IActionResult result = await controller.Edit(1, new Breed { Id = 1, Name = "Test Breed with Negative Parent Breed", BreedId = -1 });

                CheckMessage(result, $"The Parent Breed ID cannot be negative or greater than {int.MaxValue}.");
            }
        }
        public void Get_CallsRepositoryGetAllMethod()
        {
            // arrange
            var breedsController = new BreedsController(_repository);

            // act
            breedsController.Get();

            // assert
            _repository.AssertWasCalled(X => X.GetAll());
        }
Пример #14
0
        public async void GetDeleteNullId()
        {
            using (var database = new TestDb())
            {
                var controller = new BreedsController(database.Context);

                IActionResult result = await controller.Delete(null);

                CheckMessage(result, "The Breed ID provided was invalid.");
            }
        }
Пример #15
0
        public async void GetDeleteNegativeId()
        {
            using (var database = new TestDb())
            {
                var controller = new BreedsController(database.Context);

                IActionResult result = await controller.Delete(-1);

                CheckMessage(result, $"The Breed ID must be between 1 and {int.MaxValue}.");
            }
        }
Пример #16
0
        public async void PostEditParentBreedDoesntExist()
        {
            using (var database = new TestDb())
            {
                var controller = new BreedsController(database.Context);

                IActionResult result = await controller.Edit(1, new Breed { Id = 1, Name = "Test Breed with Invalid Parent Breed", BreedId = 500 });

                BreedViewModel viewModel = CheckViewModel(result);

                CheckMessage(result, "The parent Breed selected could not be found.");
            }
        }
        public void Get_ReturnsListOfBreedOrderedAlphabeticallyAscending()
        {
            // arrange
             var breedsController = new BreedsController(_repository);

             // act
             var result = breedsController.Get().ToList();

             // assert
             Assert.That(result, Is.TypeOf<List<Breed>>());
             Assert.That(result.First().Name, Is.EqualTo("Afghan Hound"));
             Assert.That(result.Last().Name, Is.EqualTo("Whippet"));
        }
Пример #18
0
        public async void PostDeleteConfirmNegativeId()
        {
            using (var database = new TestDb())
            {
                var controller = new BreedsController(database.Context);

                IActionResult result = await controller.DeleteConfirmed(-1);

                RedirectToActionResult redirect = Assert.IsType <RedirectToActionResult>(result);

                Assert.Equal("Index", redirect.ActionName);
            }
        }
Пример #19
0
        public async void GetIndex()
        {
            using (var database = new TestDb())
            {
                var controller = new BreedsController(database.Context);

                IActionResult result = await controller.Index();

                ViewResult          viewResult = Assert.IsType <ViewResult>(result);
                IEnumerable <Breed> breeds     = Assert.IsAssignableFrom <IEnumerable <Breed> >(viewResult.ViewData.Model);

                Assert.Equal(database.Context.Breeds.Count(), breeds.Count());
            }
        }
Пример #20
0
        public void GetCreate()
        {
            using (var database = new TestDb())
            {
                var controller = new BreedsController(database.Context);

                IActionResult result = controller.Create();

                BreedViewModel viewModel = CheckViewModel(result);

                Assert.Equal(0, viewModel.Breed.Id);
                Assert.True(string.IsNullOrEmpty(viewModel.Breed.Name));
            }
        }
Пример #21
0
        public async void GetDeleteSuccess()
        {
            using (var database = new TestDb())
            {
                var controller = new BreedsController(database.Context);

                IActionResult result = await controller.Delete(3);

                BreedViewModel viewModel = CheckViewModel(result);
                Breed          breed     = database.Context.Breeds.SingleOrDefault(b => b.Id == 3);

                Assert.Equal(3, viewModel.Breed.Id);
                Assert.Equal(breed.Name, viewModel.Breed.Name);
            }
        }
Пример #22
0
        public async void GetDetailsWithIndividuals()
        {
            using (var database = new TestDb())
            {
                var controller = new BreedsController(database.Context);

                IActionResult result = await controller.Details(8);

                BreedViewModel viewModel = CheckViewModel(result);
                Breed          breed     = database.Context.Breeds.SingleOrDefault(b => b.Id == 8);

                Assert.Equal(8, viewModel.Breed.Id);
                Assert.Equal(breed.Name, viewModel.Breed.Name);
                Assert.Equal(viewModel.Individuals.Count(), database.Context.Individuals.Count(m => m.Breed.Id == 8));
            }
        }
Пример #23
0
        public async void PostEditParentBreedNull()
        {
            using (var database = new TestDb())
            {
                var controller = new BreedsController(database.Context);

                IActionResult result = await controller.Edit(10, new Breed { Id = 10, Name = "Test Breed with Null Parent Breed", BreedId = null });

                RedirectToActionResult redirect = Assert.IsType <RedirectToActionResult>(result);

                Assert.Equal("Index", redirect.ActionName);

                Breed breed = database.Context.Breeds.SingleOrDefault(b => b.Id == 10);

                Assert.Equal("Test Breed with Null Parent Breed", breed.Name);
            }
        }
Пример #24
0
        public async void PostEditParentBreedZero()
        {
            using (var database = new TestDb())
            {
                var controller = new BreedsController(database.Context);

                IActionResult result = await controller.Edit(1, new Breed { Id = 1, Name = "Test Modified Breed with Zero Parent Breed", BreedId = 0 });

                RedirectToActionResult redirect = Assert.IsType <RedirectToActionResult>(result);

                Assert.Equal("Index", redirect.ActionName);

                Breed breed = database.Context.Breeds.SingleOrDefault(b => b.Name == "Test Modified Breed with Zero Parent Breed");

                Assert.Equal(1, breed.Id);
            }
        }
Пример #25
0
        public async void PostEditSuccess()
        {
            using (var database = new TestDb())
            {
                var controller = new BreedsController(database.Context);

                IActionResult result = await controller.Edit(6, new Breed(6, "Modified Breed"));

                RedirectToActionResult redirect = Assert.IsType <RedirectToActionResult>(result);

                Assert.Equal("Index", redirect.ActionName);

                Breed breed = database.Context.Breeds.SingleOrDefault(b => b.Id == 6);

                Assert.Equal("Modified Breed", breed.Name);
            }
        }
Пример #26
0
        public async void PostCreateParentBreedNull()
        {
            using (var database = new TestDb())
            {
                var controller = new BreedsController(database.Context);

                IActionResult result = await controller.Create(new Breed { Name = "Test Breed with Null Parent Breed", BreedId = null });

                RedirectToActionResult redirect = Assert.IsType <RedirectToActionResult>(result);

                Assert.Equal("Index", redirect.ActionName);

                Breed breed = database.Context.Breeds.LastOrDefault();

                Assert.Equal("Test Breed with Null Parent Breed", breed.Name);
            }
        }
Пример #27
0
        public async void PostCreateSuccess()
        {
            using (var database = new TestDb())
            {
                var controller = new BreedsController(database.Context);

                IActionResult result = await controller.Create(new Breed("Test Breed 11"));

                RedirectToActionResult redirect = Assert.IsType <RedirectToActionResult>(result);

                Assert.Equal("Index", redirect.ActionName);

                Breed breed = database.Context.Breeds.LastOrDefault();

                Assert.Equal("Test Breed 11", breed.Name);
            }
        }
Пример #28
0
        public async void PostCreateWithParentBreedSuccess()
        {
            using (var database = new TestDb())
            {
                var controller = new BreedsController(database.Context);

                IActionResult result = await controller.Create(new Breed { Name = "Test Breed with Parent Breed", BreedId = 1 });

                RedirectToActionResult redirect = Assert.IsType <RedirectToActionResult>(result);

                Assert.Equal("Index", redirect.ActionName);

                Breed breed = database.Context.Breeds.SingleOrDefault(b => b.Name == "Test Breed with Parent Breed");

                Assert.Equal("Test Breed with Parent Breed", breed.Name);
                Assert.Equal(1, breed.ParentBreed.Id);
            }
        }
Пример #29
0
        public async void PostDeleteConfirmSuccess()
        {
            using (var database = new TestDb())
            {
                Assert.True(database.Context.Breeds.Any(i => i.Id == 9));
                Assert.False(database.Context.Breeds.Any(b => b.BreedId == 9));
                Assert.False(database.Context.Individuals.Any(b => b.BreedId == 9));

                var controller = new BreedsController(database.Context);

                IActionResult result = await controller.DeleteConfirmed(9);

                RedirectToActionResult redirect = Assert.IsType <RedirectToActionResult>(result);

                Assert.Equal("Index", redirect.ActionName);

                Assert.False(database.Context.Breeds.Any(i => i.Id == 9));
            }
        }
        public void GetAllBreedsErrorNotFoundTest()
        {
            //Arrange
            List <Breed> expectedBreeds = null;

            var mockBreedsBusinessLogic = new Mock <IBreedsBusinessLogic>();

            mockBreedsBusinessLogic
            .Setup(bl => bl.GetAllBreeds())
            .Returns(expectedBreeds);

            var controller = new BreedsController(mockBreedsBusinessLogic.Object);

            //Act
            IHttpActionResult obtainedResult = controller.Get();

            //Assert
            mockBreedsBusinessLogic.VerifyAll();
            Assert.IsInstanceOfType(obtainedResult, typeof(NotFoundResult));
        }
        public void UpdateBreedWithNullIdErrorTest()
        {
            //Arrange
            Breed fakeBreed = null;

            var mockBreedsBusinessLogic = new Mock <IBreedsBusinessLogic>();

            mockBreedsBusinessLogic
            .Setup(bl => bl.Update(new Guid(), It.IsAny <Breed>()))
            .Throws(new ArgumentNullException());

            var controller = new BreedsController(mockBreedsBusinessLogic.Object);

            //Act
            IHttpActionResult obtainedResult = controller.Put(new Guid(), fakeBreed);

            //Assert
            mockBreedsBusinessLogic.VerifyAll();
            Assert.IsInstanceOfType(obtainedResult, typeof(BadRequestErrorMessageResult));
        }
        public void GetBreedByIdNotFoundErrorTest()
        {
            //Arrange
            var fakeGuid = Guid.NewGuid();

            var mockBreedsBusinessLogic = new Mock <IBreedsBusinessLogic>();

            mockBreedsBusinessLogic
            .Setup(bl => bl.GetByID(fakeGuid))
            .Returns((Breed)null);

            // Debemos retornar null, es lo que le exigimos al Mock para lograr
            // que el controller nos de NotFound

            var controller = new BreedsController(mockBreedsBusinessLogic.Object);

            //Act
            IHttpActionResult obtainedResult = controller.Get(fakeGuid);

            //Assert
            mockBreedsBusinessLogic.VerifyAll();
            Assert.IsInstanceOfType(obtainedResult, typeof(NotFoundResult));
        }