Exemplo n.º 1
0
        public void GetAllAnimals_RepositoryGetAllAnimalsThrowsException_ExpectedAnimalsReturned()
        {
            //Arrange
            var expectedMessage    = "expectedExceptionMessage";
            var expectedStatusCode = (int)HttpStatusCode.InternalServerError;

            var fakeRepository = A.Fake <IAnimalRepository>();

            A.CallTo(() => fakeRepository.GetAllAnimals()).Throws(new Exception(expectedMessage));
            var sut = new AnimalsController(fakeRepository);

            //Act
            var result         = sut.GetAllAnimals();
            var okObjectResult = (StatusCodeResult)result;

            //Assert
            Assert.NotNull(okObjectResult);
            Assert.Equal(expectedStatusCode, okObjectResult.StatusCode);
        }
Exemplo n.º 2
0
        public void Mock_ConfirmEntry_Test()
        {
            DbSetup();
            AnimalsController controller = new AnimalsController(mock.Object);
            Animal            testAnimal = new Animal();

            testAnimal.AnimalId         = 1;
            testAnimal.Name             = "Ellie";
            testAnimal.Species          = "Elephant";
            testAnimal.Sex              = "Female";
            testAnimal.HabitatType      = "Savanna";
            testAnimal.MedicalEmergency = false;
            testAnimal.VeterinarianId   = 1;

            ViewResult indexView  = controller.Index() as ViewResult;
            var        collection = indexView.ViewData.Model as List <Animal>;

            CollectionAssert.Contains(collection, testAnimal);
        }
        public void Mock_GetDetails_ReturnsView()
        {
            // Arrange
            Animal testAnimal = new Animal
            {
                AnimalId = 4,
                Name     = "donkey"
            };

            DbSetup();
            AnimalsController controller = new AnimalsController(mock.Object);

            // Act
            var resultView = controller.Details(testAnimal.AnimalId) as ViewResult;
            var model      = resultView.ViewData.Model as Animal;

            // Assert
            Assert.IsInstanceOfType(resultView, typeof(ViewResult));
            Assert.IsInstanceOfType(model, typeof(Animal));
        }
Exemplo n.º 4
0
        public async Task Create_ReturnsAnimalViewResult_WhenModelStateIsInvalid()
        {
            //Arrange
            var repository = A.Fake <IRepository <Animal> >();
            var env        = A.Fake <IWebHostEnvironment>();

            var controller = new AnimalsController(repository, env);

            controller.ModelState.AddModelError("Name", "Required");

            var animal = GetAnimal();

            //Act
            var result = await controller.Create(animal);

            //Assert
            var animalResult = Assert.IsType <ViewResult>(result);

            Assert.IsType <Animal>(animalResult.Model);
        }
Exemplo n.º 5
0
        public async Task EditGet_ReturnsViewData_WhenAnimalIsValid()
        {
            // Arrange
            var repository = A.Fake <IRepository <Animal> >();
            var env        = A.Fake <IWebHostEnvironment>();

            var    controller = new AnimalsController(repository, env);
            Animal animal     = GetAnimal();

            A.CallTo(() => repository.Get(A <int> .Ignored)).Returns(animal);

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

            //Assert
            A.CallTo(() => repository.Get(A <int> .Ignored)).MustHaveHappened();
            var viewResult = Assert.IsType <ViewResult>(result);

            Assert.IsType <Animal>(viewResult.Model);
        }
Exemplo n.º 6
0
        public async Task Create_AddsAnimalAndRedirectsToIndex_WhenModelStateIsValid()
        {
            //Arrange
            var repository = A.Fake <IRepository <Animal> >();
            var env        = A.Fake <IWebHostEnvironment>();

            var controller = new AnimalsController(repository, env);
            var animal     = GetAnimal();

            //Act
            var result = await controller.Create(animal);

            //Assert
            A.CallTo(() => repository.Create(A <Animal> .Ignored)).MustHaveHappened();

            var redirectToActionResult = Assert.IsType <RedirectToActionResult>(result);

            Assert.Null(redirectToActionResult.ControllerName);
            Assert.Equal("Index", redirectToActionResult.ActionName);
        }
        public void DB_CreatesNewEntries_Collection()
        {
            // Arrange
            AnimalsController controller = new AnimalsController(db);
            Animal            testAnimal = new Animal();

            testAnimal.Name             = "Test Animal";
            testAnimal.MedicalEmergency = false;
            testAnimal.VeterinarianId   = 1;
            testAnimal.Sex         = "female";
            testAnimal.Species     = "NajaNaja";
            testAnimal.HabitatType = "forest";


            // Act
            controller.Create(testAnimal);
            var collection = (controller.Index() as ViewResult).ViewData.Model as List <Animal>;

            // Assert
            CollectionAssert.Contains(collection, testAnimal);
        }
Exemplo n.º 8
0
        public async Task Details_AnimalReturnsToIndex_WhenIdIsValid()
        {
            // Arrange
            var repository = A.Fake <IRepository <Animal> >();
            var env        = A.Fake <IWebHostEnvironment>();

            var controller = new AnimalsController(repository, env);

            A.CallTo(() => repository.Get(A <int> .That.IsEqualTo(1))).Returns(GetAnimal());

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

            //Assert
            A.CallTo(() => repository.Get(A <int> .Ignored)).MustHaveHappened();

            var viewResult = Assert.IsType <ViewResult>(result);

            Assert.NotNull(viewResult.Model);
            Assert.IsType <Animal>(viewResult.Model);
        }
Exemplo n.º 9
0
        public void DeleteAnimalTest()
        {
            List <Animal> animalList = new List <Animal>
            {
                new Sheep(0, "Dory", 0),
                new Sheep(1, "Clone", 0)
            };
            User user = new User
            {
                ID      = 0,
                Animals = new List <int> {
                    0, 1
                },
                Name = "UserMocked"
            };
            List <User> userList = new List <User>();

            userList.Add(user);

            var mockGet = new Mock <IGetDataServices>();

            mockGet.Setup(repo => repo.GetAnimals()).Returns(animalList);
            mockGet.Setup(ga => ga.GetAnimalById(0)).Returns(animalList[0]);
            mockGet.Setup(gu => gu.GetUsers()).Returns(userList);

            var mockSet = new Mock <ISetDataServices>();

            mockSet.Setup(m => m.DeleteAnimal(0, animalList, user, userList)).Returns(true);

            // arrange
            var controller = new AnimalsController(mockGet.Object, mockSet.Object);

            // act
            var result        = controller.DeleteAnimal(0);
            var deletedResult = result as OkObjectResult;

            // assert
            Assert.IsNotNull(deletedResult);
            Assert.AreEqual(200, deletedResult.StatusCode);
        }
Exemplo n.º 10
0
        public async Task DeleteConfirmed_DeleteAnimalsAndReturnsIndex_WhenAnimalIsValid()
        {
            // Arrange
            var repository = A.Fake <IRepository <Animal> >();
            var env        = A.Fake <IWebHostEnvironment>();

            var    controller = new AnimalsController(repository, env);
            Animal animal     = GetAnimal();

            A.CallTo(() => repository.Get(1)).Returns(animal);

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

            //Assert
            A.CallTo(() => repository.Delete(animal)).MustHaveHappened();

            var redirectToActionResult = Assert.IsType <RedirectToActionResult>(result);

            Assert.Null(redirectToActionResult.ControllerName);
            Assert.Equal("Index", redirectToActionResult.ActionName);
        }
Exemplo n.º 11
0
        public void GetAnimalsTestWithoutAnimals()
        {
            // arrange
            var mockGet = new Mock <IGetDataServices>();

            mockGet.Setup(repo => repo.GetAnimals()).Returns(new List <Animal>());

            var mockSet = new Mock <ISetDataServices>();

            mockSet.Setup(m => m.UpdateAnimals(new List <Animal>())).Returns(new List <Animal>());

            // arrange
            var controller = new AnimalsController(mockGet.Object, mockSet.Object);

            // act
            var result   = controller.GetAnimals();
            var okResult = result as OkObjectResult;

            // assert
            Assert.IsNotNull(okResult);
            Assert.AreEqual(200, okResult.StatusCode);
            Assert.AreEqual("There is no animals yet", okResult.Value);
        }
Exemplo n.º 12
0
        public async Task Details_ReturnsNotFoundResult_WhenIdIsNull()
        {
            var connection = new SqliteConnection("DataSource=:memory:");

            connection.Open();
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseSqlite(connection)
                          .Options;

            using (var context = new ApplicationDbContext(options))
            {
                context.Database.EnsureCreated();
                context.SaveChanges();
            }
            using (var context = new ApplicationDbContext(options))
            {
                var controller = new AnimalsController(context);

                var result = await controller.Details(null);

                Assert.IsType <NotFoundResult>(result);
            }
        }
Exemplo n.º 13
0
        public void GetAllAnimals_RepositoryIsSetup_ExpectedAnimalsReturned()
        {
            //Arrange
            var expectedList = new List <IAnimal>
            {
                new Cat {
                    Age = 5, Name = "Trasan"
                },
                new Giraffe {
                    Age = 8, Name = "Långben"
                },
                new Sparrow {
                    Name = "Fjäder"
                },
                new Sparrow {
                    Name = "Flygarn"
                }
            };

            var expectedStatusCode = 200;
            var expectedNumber     = expectedList.Count;

            var fakeRepository = A.Fake <IAnimalRepository>();

            A.CallTo(() => fakeRepository.GetAllAnimals()).Returns(expectedList);
            var sut = new AnimalsController(fakeRepository);

            //Act
            var result         = sut.GetAllAnimals();
            var okObjectResult = (OkObjectResult)result;

            //Assert
            Assert.NotNull(okObjectResult);
            Assert.Equal(expectedStatusCode, okObjectResult.StatusCode);
            Assert.True(okObjectResult.Value is List <IAnimal>);
            Assert.Equal(expectedNumber, ((List <IAnimal>)okObjectResult.Value).Count);
        }
Exemplo n.º 14
0
        public void GetAnimalsFromUserIsUserTest()
        {
            List <Animal> animalList = new List <Animal>
            {
                new Sheep(0, "Dory", 0),
                new Sheep(1, "Clone", 0)
            };
            User u = new User
            {
                ID      = 0,
                Animals = new List <int> {
                    0, 1
                },
                Name = "UserMocked"
            };

            var mockGet = new Mock <IGetDataServices>();

            mockGet.Setup(repo => repo.GetAnimals()).Returns(animalList);
            mockGet.Setup(gu => gu.GetUserById(0)).Returns(u);

            var mockSet = new Mock <ISetDataServices>();

            mockSet.Setup(m => m.UpdateAnimals(animalList)).Returns(animalList);

            // arrange
            var controller = new AnimalsController(mockGet.Object, mockSet.Object);

            // act
            var result   = controller.GetAnimalsFromUser(0);
            var okResult = result as OkObjectResult;

            // assert
            Assert.IsNotNull(okResult);
            Assert.AreEqual(200, okResult.StatusCode);
            Assert.ReferenceEquals(animalList, okResult.Value);
        }
Exemplo n.º 15
0
        public void CreateAnimalWithWrongParametersTest()
        {
            List <Animal> animalList = new List <Animal>
            {
                new Sheep(0, "Dory", 0),
                new Sheep(1, "Clone", 0)
            };
            Animal      animalCreated = new Sheep(2, "CloneTwo", 0);
            List <User> listUser      = new List <User>();

            var mockGet = new Mock <IGetDataServices>();

            mockGet.Setup(repo => repo.GetAnimals()).Returns(animalList);
            mockGet.Setup(gu => gu.GetUsers()).Returns(listUser);
            mockGet.Setup(gu => gu.GetUserById(25)).Returns((User)null);

            var mockSet = new Mock <ISetDataServices>();

            mockSet.Setup(m => m.CreateAnimal("CloneTwo", 2, animalList, null, listUser)).Returns((Animal)null);

            // arrange
            var controller = new AnimalsController(mockGet.Object);

            // act
            var result = controller.CreateAnimal(new PayloadCreate
            {
                Name   = "CloneTwo",
                Type   = 2,
                UserId = 25
            });
            var BadResult = result as BadRequestObjectResult;

            // assert
            Assert.IsNotNull(BadResult);
            Assert.AreEqual(400, BadResult.StatusCode);
            Assert.ReferenceEquals("There is no user with ID = 25", BadResult.Value);
        }
Exemplo n.º 16
0
        public void DB_CreateNewEntry_test()
        {
            AnimalsController controller = new AnimalsController(db);
            //VeterinariansController controller1 = new VeterinariansController(db);
            //Veterinarian testVet = new Veterinarian("Jesse", "Elephants");
            Animal testAnimal = new Animal();

            testAnimal.AnimalId         = 1;
            testAnimal.Name             = "Ellie";
            testAnimal.Species          = "Elephant";
            testAnimal.Sex              = "Female";
            testAnimal.HabitatType      = "Savanna";
            testAnimal.MedicalEmergency = false;
            testAnimal.VeterinarianId   = 1;

            //controller1.Create(testVet);

            controller.Create(testAnimal);
            var collection = (controller.Index() as ViewResult).ViewData.Model as List <Animal>;



            CollectionAssert.Contains(collection, testAnimal);
        }
Exemplo n.º 17
0
        public void GetAnimalsFromUserWithoutAnimalsTest()
        {
            List <Animal> animalList = new List <Animal>
            {
                new Sheep(0, "Dory", 1),
                new Sheep(1, "Clone", 1)
            };
            User user = new User
            {
                ID      = 0,
                Animals = new List <int> {
                },
                Name    = "UserMocked"
            };

            var mockGet = new Mock <IGetDataServices>();

            mockGet.Setup(repo => repo.GetAnimals()).Returns(animalList);
            mockGet.Setup(gu => gu.GetUserById(0)).Returns(user);

            var mockSet = new Mock <ISetDataServices>();

            mockSet.Setup(m => m.UpdateAnimals(animalList)).Returns(animalList);

            // arrange
            var controller = new AnimalsController(mockGet.Object, mockSet.Object);

            // act
            var result         = controller.GetAnimalsFromUser(0);
            var NotFoundResult = result as NotFoundObjectResult;

            // assert
            Assert.IsNotNull(NotFoundResult);
            Assert.AreEqual(404, NotFoundResult.StatusCode);
            Assert.ReferenceEquals("That user has no animals", NotFoundResult.Value);
        }
Exemplo n.º 18
0
        public void SetupContext()
        {
            #region population of animals & its props

            skins = new List <Skin>()
            {
                new Skin()
                {
                    Id = 1, SkinColor = "red"
                },
                new Skin()
                {
                    Id = 2, SkinColor = "blue"
                },
                new Skin()
                {
                    Id = 3, SkinColor = "white"
                },
                new Skin()
                {
                    Id = 4, SkinColor = "black"
                }
            };

            kinds = new List <Kind>()
            {
                new Kind()
                {
                    Id = 1, AnimalKind = "fish"
                },
                new Kind()
                {
                    Id = 2, AnimalKind = "pet"
                },
                new Kind()
                {
                    Id = 3, AnimalKind = "bird"
                },
                new Kind()
                {
                    Id = 4, AnimalKind = "reptile"
                }
            };

            locations = new List <Location>()
            {
                new Location()
                {
                    Id = 1, LocationName = "park"
                },
                new Location()
                {
                    Id = 2, LocationName = "home"
                },
                new Location()
                {
                    Id = 3, LocationName = "farm"
                },
                new Location()
                {
                    Id = 4, LocationName = "lake"
                }
            };

            regions = new List <Region>()
            {
                new Region()
                {
                    Id = 1, RegionName = "Asia"
                },
                new Region()
                {
                    Id = 2, RegionName = "America"
                },
                new Region()
                {
                    Id = 3, RegionName = "Europa"
                },
                new Region()
                {
                    Id = 4, RegionName = "Australia"
                },
                new Region()
                {
                    Id = 5, RegionName = "Africa"
                }
            };

            animals = new List <Animal>()
            {
                new Animal()
                {
                    AnimalId   = 1,
                    AnimalName = "pig",
                    SkinId     = skins[0].Id, Skin = skins[0],
                    KindId     = kinds[2].Id, Kind = kinds[2],
                    LocationId = locations[1].Id, Location = locations[1],
                    Regions    = new List <Region> {
                        regions[0], regions[3]
                    }
                },
                new Animal()
                {
                    AnimalId   = 2,
                    AnimalName = "crocodile",
                    SkinId     = skins[3].Id, Skin = skins[3],
                    KindId     = kinds[0].Id, Kind = kinds[0],
                    LocationId = locations[2].Id, Location = locations[2],
                    Regions    = new List <Region> {
                        regions[2], regions[3], regions[4]
                    }
                },
                new Animal()
                {
                    AnimalId   = 3,
                    AnimalName = "bear",
                    SkinId     = skins[1].Id, Skin = skins[1],
                    KindId     = kinds[0].Id, Kind = kinds[0],
                    LocationId = locations[3].Id, Location = locations[3],
                    Regions    = new List <Region> {
                        regions[1], regions[4]
                    }
                },
                new Animal()
                {
                    AnimalId   = 4,
                    AnimalName = "horse",
                    SkinId     = skins[3].Id, Skin = skins[3],
                    KindId     = kinds[3].Id, Kind = kinds[3],
                    LocationId = locations[2].Id, Location = locations[2],
                    Regions    = new List <Region> {
                        regions[0], regions[1], regions[4]
                    }
                },
                new Animal()
                {
                    AnimalId   = 5,
                    AnimalName = "cat",
                    SkinId     = skins[0].Id, Skin = skins[0],
                    KindId     = kinds[2].Id, Kind = kinds[2],
                    LocationId = locations[0].Id, Location = locations[0],
                    Regions    = new List <Region> {
                        regions[0], regions[3], regions[4]
                    }
                },
                new Animal()
                {
                    AnimalId   = 6,
                    AnimalName = "swallow",
                    SkinId     = skins[0].Id, Skin = skins[0],
                    KindId     = kinds[2].Id, Kind = kinds[2],
                    LocationId = locations[0].Id, Location = locations[0],
                    Regions    = new List <Region> {
                        regions[0], regions[2], regions[3], regions[4]
                    }
                },
                new Animal()
                {
                    AnimalId   = 7,
                    AnimalName = "fox",
                    SkinId     = skins[3].Id, Skin = skins[3],
                    KindId     = kinds[3].Id, Kind = kinds[3],
                    LocationId = locations[2].Id, Location = locations[2],
                    Regions    = new List <Region> {
                        regions[0], regions[1], regions[4]
                    }
                },
                new Animal()
                {
                    AnimalId   = 8,
                    AnimalName = "monkey",
                    SkinId     = skins[0].Id, Skin = skins[0],
                    KindId     = kinds[2].Id, Kind = kinds[2],
                    LocationId = locations[0].Id, Location = locations[0],
                    Regions    = new List <Region> {
                        regions[0], regions[1], regions[3], regions[4]
                    }
                },
                new Animal()
                {
                    AnimalId   = 9,
                    AnimalName = "kengoo",
                    SkinId     = skins[0].Id, Skin = skins[0],
                    KindId     = kinds[2].Id, Kind = kinds[2],
                    LocationId = locations[0].Id, Location = locations[0],
                    Regions    = new List <Region> {
                        regions[0], regions[3], regions[4]
                    }
                },
                new Animal()
                {
                    AnimalId   = 10,
                    AnimalName = "hawk",
                    SkinId     = skins[2].Id, Skin = skins[2],
                    KindId     = kinds[3].Id, Kind = kinds[3],
                    LocationId = locations[0].Id, Location = locations[0],
                    Regions    = new List <Region> {
                        regions[1], regions[2], regions[3]
                    }
                }
            };

            #endregion population of animals & its props

            #region Setup MOQs

            mockSkin = new Mock <IRepository <Skin> >();
            mockSkin.Setup(a => a.GetAll())
            .Returns(skins.AsQueryable <Skin>);

            mockKind = new Mock <IRepository <Kind> >();
            mockKind.Setup(a => a.GetAll())
            .Returns(kinds.AsQueryable <Kind>);

            mockLocation = new Mock <IRepository <Location> >();
            mockLocation.Setup(a => a.GetAll())
            .Returns(locations.AsQueryable <Location>);

            mockRegion = new Mock <IRepository <Region> >();
            mockRegion.Setup(a => a.GetAll())
            .Returns(regions.AsQueryable <Region>);

            mockAnimal = new Mock <IRepository <Animal> >();
            mockAnimal.Setup(a => a.GetAll())
            .Returns(animals.AsQueryable <Animal>);

            #endregion Setup MOQs

            #region Setup mockFactory

            mockFactory = new Mock <IRepositoryFactory>();

            mockFactory.Setup <IRepository <Skin> >(m => m.CreateSkinRepository()).Returns(mockSkin.Object);
            mockFactory.Setup <IRepository <Kind> >(m => m.CreateKindRepository()).Returns(mockKind.Object);
            mockFactory.Setup <IRepository <Location> >(m => m.CreateLocationRepository()).Returns(mockLocation.Object);
            mockFactory.Setup <IRepository <Region> >(m => m.CreateRegionRepository()).Returns(mockRegion.Object);
            mockFactory.Setup <IRepository <Animal> >(m => m.CreateAnimalRepository()).Returns(mockAnimal.Object);

            #endregion Setup mockFactory

            controller = new AnimalsController(mockFactory.Object);
        }