Ejemplo n.º 1
0
        public void PersonController_CreateAnObject()
        {
            //Arrange
            string expected = "PersonsController";
            var    mock     = new Mock <IPersonService>();

            //Act
            PersonsController controller = new PersonsController(mock.Object);

            //Assert
            Assert.IsNotNull(controller);
            Assert.AreEqual(expected, controller.GetType().Name);
        }
Ejemplo n.º 2
0
        public void DeleteTest()
        {
            int id   = 5;
            var mock = new Mock <IRepository <Person> >();

            mock.Setup(r => r.Remove(id)).Returns(GetTestPersons().FirstOrDefault(p => p.Id == id));
            var controller = new PersonsController(mock.Object);

            var act = controller.Delete(id);

            Assert.NotNull(act.Value);
            Assert.IsType <OkObjectResult>(act.Result);
        }
Ejemplo n.º 3
0
        public async Task Persons_Get_All()
        {
            // Arrange
            var controller = new PersonsController(new PersonService());

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

            // Assert
            var okResult = result.Should().BeOfType <OkObjectResult>().Subject;
            var persons  = okResult.Value.Should().BeAssignableTo <IEnumerable <Person> >().Subject;

            persons.Count().Should().Be(50);
        }
        private PersonsController CreatePersonsController(HttpRequestMessage httpRequest, IPersonRepository personsRepository)
        {
            PersonsManager personsManager = new PersonsManager(userManager.Object, cacheStoreManager, mockLogManager.Object, personsRepository, transformationManager);

            PersonAPIManager personAPIManager = new PersonAPIManager(userManager.Object, cacheStoreManager, mockLogManager.Object, personsManager);

            PersonsController personsController = new PersonsController(userManager.Object, personAPIManager)
            {
                Request       = httpRequest,
                Configuration = new HttpConfiguration(),
            };

            return(personsController);
        }
Ejemplo n.º 5
0
        public void GetAllTest()
        {
            var mock = new Mock <IRepository <Person> >();

            mock.Setup(p => p.GetAll()).Returns(GetTestPersons);
            var controller = new PersonsController(mock.Object);

            var act = controller.Get();

            var model = Assert.IsAssignableFrom <IEnumerable <Person> >(act);

            Assert.NotNull(act);
            Assert.Equal(act.Count(), model.Count());
        }
Ejemplo n.º 6
0
        public async Task Persons_Get_Specific()
        {
            // Arrange
            var controller = new PersonsController(new PersonService());

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

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

            person.Id.Should().Be(16);
        }
Ejemplo n.º 7
0
        public async Task DeleteShouldCallDeletePersonInBlOnce()
        {
            //Arrange
            var mock       = new Mock <IPersonService>();
            var controller = new PersonsController(mock.Object);

            mock.Setup(x => x.Delete(1));

            //Act
            await controller.DeletePerson(1);

            //Assert
            mock.Verify(x => x.DeleteAsync(1), Times.Once);
        }
Ejemplo n.º 8
0
        public void Initialize()
        {
            personServiceMock = new Mock <IPersonService>();
            objController     = new PersonsController(personServiceMock.Object);

            person = new Person
            {
                Id        = 77,
                Address   = "P.O. Box 795, 1956 Odio. Rd.",
                DateBirth = new DateTime(1980, 5, 5),
                Email     = "*****@*****.**",
                Gender    = GenderType.Male,
                Name      = "Branden Johnston"
            };
        }
        public PersonsControllerTest()
        {
            _mockLogger = new Mock <ILogger <PersonsController> >();

            //auto mapper configuration
            var mapperConfig = new MapperConfiguration(cfg =>
            {
                cfg.AddProfile(new MappingProfile());
            });
            var mapper = mapperConfig.CreateMapper();

            _mockPersonService = new Mock <IPersonService>();

            _controller = new PersonsController(_mockPersonService.Object, mapper, _mockLogger.Object);
        }
Ejemplo n.º 10
0
        public async Task GetPerson_When_User_Is_Not_Found_Will_Return_Not_Found()
        {
            //Arrange
            var fakePersonId = 0;

            _personRepositoryMock.Setup(e => e.GetPersonAsync(fakePersonId))
            .Returns(Task.FromResult <Person>(null));

            //Act
            var personsController = new PersonsController(_loggerMock.Object, _personRepositoryMock.Object);
            var actionResult      = await personsController.GetPersonAsync(fakePersonId);

            //Assert
            Assert.IsType <NotFoundObjectResult>(actionResult.Result);
        }
Ejemplo n.º 11
0
        public void Values_Get_All()
        {
            // Arrange
            var controller = new PersonsController(new PersonService());

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

            // Assert

            /* var okResult = result.Should().BeOfType<OkObjectResult>().Subject;
             * var persons = okResult.Value.Should().BeAssignableTo<IEnumerable<Person>>().Subject;
             *
             * persons.Count().Should().Be(50);*/
        }
Ejemplo n.º 12
0
        public void GetTest()
        {
            var mock = new Mock <IRepository <Person> >();

            mock.Setup(rep => rep.Find(1)).Returns(new Person {
                Id = 1, Name = "Tom", DisplayName = "a", Skills = new Skill[5]
            });
            var controller = new PersonsController(mock.Object);

            var act = controller.Get(1);

            Assert.NotNull(act);
            Assert.IsAssignableFrom <ActionResult <Person> >(act);
            Assert.IsType <OkObjectResult>(act.Result);
        }
Ejemplo n.º 13
0
        public void Values_Get_Specific()
        {
            // Arrange
            var controller = new PersonsController(new PersonService());

            // Act
            object p      = GetP(controller);
            var    result = p;

            // Assert

            /*  var okResult = result.Should().BeOfType<OkObjectResult>().Subject;
             * var person = okResult.Value.Should().BeAssignableTo<Person>().Subject;
             * person.Id.Should().Be(16);*/
        }
Ejemplo n.º 14
0
        public async Task Values_Get_Id16()
        {
            //Arrange
            var       controller = new PersonsController(new PersonService());
            const int personId   = 16;

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

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

            person.Id.Should().Be(personId);
        }
Ejemplo n.º 15
0
        public void Chech_Valid_Return_OkResult_CreatePerson()
        {
            //Arrange

            var controller = new PersonsController(_mockRepo.Object, _mapper, _mockLogger.Object);
            var person     = new PersonCreateDto()
            {
                Name = "Peter"
            };

            //Act
            var data = controller.CreatePerson(person);

            //Assert
            Assert.IsInstanceOf <CreatedAtRouteResult>(data.Result);
        }
Ejemplo n.º 16
0
        public async Task Persons_Delete()
        {
            //Arrange
            var       service        = new PersonService();
            var       controller     = new PersonsController(service);
            const int deletePersonId = 20;

            //Act
            var result = await controller.Delete(deletePersonId);

            //Assert
            var    okResult = result.Should().BeOfType <NoContentResult>().Subject;
            Action action   = () => service.Get(deletePersonId);

            action.Should().Throw <InvalidOperationException>();
        }
Ejemplo n.º 17
0
        public async Task CreateNewContact()
        {
            var options = new DbContextOptionsBuilder <PhoneDbContext>()
                          .UseInMemoryDatabase(databaseName: "Test_PhoneBook")
                          .Options;

            using (var inMemoryContext = new PhoneDbContext(options))
            {
                var service = new PersonsController(inMemoryContext);
                await service.Create(new Persons { Ad = "Deneme", Soyad = "deneme soyadı", Firma = "Test Firması" });
            }
            using (var inMemoryContext = new PhoneDbContext(options))
            {
                Assert.Equal(1, await inMemoryContext.Persons.CountAsync());
            }
        }
Ejemplo n.º 18
0
        public void CreatePerson()
        {
            PersonWithoutIdentifiersViewModel model = new PersonWithoutIdentifiersViewModel
            {
                FirstName = "firstname",
                LastName  = "lastname"
            };

            var controller = new PersonsController(_context);
            var result     = (ObjectResult)controller.CreatePerson(model);

            Assert.IsType <ApiResponse <bool> >(result.Value);
            var response = (ApiResponse <bool>)result.Value;

            Assert.Equal(((int)HttpStatusCode.OK).ToString(), response.StatusCode.ToString());
        }
Ejemplo n.º 19
0
        public async Task Persons_Delete()
        {
            // Arrange
            var service    = new PersonService();
            var controller = new PersonsController(service);

            // Act
            var result = await controller.Delete(20);

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

            // should throw an eception,
            // because the person with id==20 doesn't exist enymore
            AssertionExtensions.ShouldThrow <InvalidOperationException>(() => service.Get(20));
        }
        public void PersonsController_Get_Returns_OKResponseCode(PersonSearchCriteria personSearchCriteria)
        {
            #region Arrange
            SetupUserIdentity();
            SetupPersonRepository(personSearchCriteria, out Mock <IPersonRepository> personRepository, out IPaginatedList <TbPerson> expected);

            var httpRequest = new HttpRequestMessage(new HttpMethod(AppSettings.HTTPGET), $"{AppSettings.BASEURL}{RouteHelper.PersonRoutePrefix}/{personSearchCriteria}");

            PersonsController personsController = CreatePersonsController(httpRequest, personRepository.Object);

            #endregion

            #region Act

            var response = personsController.Search(personSearchCriteria);
            //var response = personsController.Get(It.IsAny<int>());
            var contentResult = response as NegotiatedContentResult <ResponsePaginatedCollection <Person> >;


            #endregion

            #region Assert

            #region Expected Data

            var expectedPerson = new Person()
            {
                PersonId    = (personSearchCriteria.PersonId) == 0 ? userIdentity.UserId : 123,
                FirstName   = "John",
                LastName    = "Smith",
                DisplayName = "John Smith"
            };

            #endregion

            Assertions.AssertOkResponse(contentResult);

            var personsData = contentResult.Content.results;
            for (int i = 0; i <= personsData.Count - 1; i++)
            {
                //Data
                var actualPerson = personsData[i].data;
                Assertions.AssertData(expectedPerson, actualPerson);
            }

            #endregion
        }
        public void Put_test()
        {
            // Arrange

            IEnumerable <Person> persons = new Person[] {
                new Person()
                {
                    FirstName = "test1", Id = 1
                },
                new Person()
                {
                    FirstName = "test1", Id = 2
                },
                new Person()
                {
                    FirstName = "test2", Id = 3
                },
                new Person()
                {
                    FirstName = "test3", Id = 4
                }
            }.ToList();

            Person person = new Person()
            {
                FirstName = "test12", Id = 1
            };

            Mock <IPersonsRepository> mock = new Mock <IPersonsRepository>();

            mock.Setup(r => r.Update(It.IsAny <Person>())).Callback <Person>((x) => persons.Where(y => y.Id == x.Id).SingleOrDefault().FirstName = x.FirstName);

            //mock.Setup(r => r.GetById(It.IsAny<int>()))
            //	.Returns<int>((a) => persons.Where(y => y.Id == a).SingleOrDefault());

            PersonsController personsController = new PersonsController(mock.Object);

            // Act

            personsController.Put(person);
            ObjectResult result = personsController.Get(person.Id) as ObjectResult;
            Person       value  = result.Value as Person;

            //Assert

            Assert.Equal(person, value);
        }
Ejemplo n.º 22
0
        public async Task Create_EqualNames_ReturnErrorMessage()
        {
            // Arrange
            persons.Setup(m => m.EqualPers(model.Name, "add", null)).Returns(GetTestPersons());
            var contr = new PersonsController(log.Object, persons.Object);

            //Act
            var result = await contr.Create(model);

            //Assert
            Assert.That(result, Is.TypeOf <ViewResult>());
            Assert.AreEqual(contr.ModelState
                            .Values
                            .First()
                            .Errors[0]
                            .ErrorMessage, "Персонаж с таким именем занят!");
        }
Ejemplo n.º 23
0
        public void Update()
        {
            var controller = new PersonsController(_context);
            PersonUpdateViewModel model = new PersonUpdateViewModel
            {
                PersonId  = _context.Person.FirstOrDefault().Id,
                FirstName = "FirstName",
                LastName  = "LastName"
            };

            var result = (ObjectResult)controller.Update(model);

            Assert.IsType <ApiResponse <bool> >(result.Value);
            var response = (ApiResponse <bool>)result.Value;

            Assert.Equal(((int)HttpStatusCode.OK).ToString(), response.StatusCode.ToString());
        }
Ejemplo n.º 24
0
        public void CreateIdentifier()
        {
            var controller = new PersonsController(_context);
            CreateIdentifierViewModel model = new CreateIdentifierViewModel
            {
                PersonId = _context.Person.FirstOrDefault().Id,
                Type     = IdentifiersType.Email,
                Value    = "*****@*****.**"
            };

            var result = (ObjectResult)controller.CreateIdentifier(model);

            Assert.IsType <ApiResponse <bool> >(result.Value);
            var response = (ApiResponse <bool>)result.Value;

            Assert.Equal(((int)HttpStatusCode.OK).ToString(), response.StatusCode.ToString());
        }
Ejemplo n.º 25
0
        public void TestGetAllPersons()
        {
            var personscontroller = new PersonsController();
            var actionResult      = personscontroller.GetAllPersons();
            var response          = actionResult as OkNegotiatedContentResult <IEnumerable <Person> >;

            Assert.IsNotNull(response);



            // Act
            //var response = controller.Get(10);

            // Assert
            //Product product;
            // Assert.IsTrue(response.TryGetContentValue<Product>(out product));
            // Assert.AreEqual(10, product.Id);
        }
Ejemplo n.º 26
0
        public async Task Should_Return_NotFound_When_Get_With_BadID()
        {
            // Arrange
            var mockRepository = new Mock <IPersonRepository>();

            mockRepository.Setup(x => x.GetPerson(testId))
            .Returns(Task.FromResult <Person>(null));

            // Act
            var controller = new PersonsController(mockRepository.Object);
            IHttpActionResult actionResult = await controller.GetPerson(testId);

            // Assert
            var customActionResult = actionResult as CustomJSONActionResult;

            Assert.NotNull(customActionResult);
            Assert.Equal(HttpStatusCode.NotFound, customActionResult.StatusCode);
        }
Ejemplo n.º 27
0
        public void PutTest()
        {
            int    id     = 1;
            Person person = new Person()
            {
                Name = "a", DisplayName = "b"
            };

            var mock = new Mock <IRepository <Person> >();

            mock.Setup(r => r.Update(id, person)).Returns(GetTestPersons().FirstOrDefault(p => p.Id == id));
            var controller = new PersonsController(mock.Object);

            var act = controller.Put(1, person);

            Assert.NotNull(act);
            Assert.IsType <OkObjectResult>(act.Result);
        }
        public void Delete_test()
        {
            // Arrange

            IEnumerable <Person> persons = new Person[] {
                new Person()
                {
                    FirstName = "test1", Id = 1
                },
                new Person()
                {
                    FirstName = "test1", Id = 2
                },
                new Person()
                {
                    FirstName = "test2", Id = 3
                },
                new Person()
                {
                    FirstName = "test3", Id = 4
                }
            }.ToList();


            Mock <IPersonsRepository> mock = new Mock <IPersonsRepository>();

            mock.Setup(r => r.Get()).Returns((persons).AsQueryable <Person>());

            PersonsController personsController = new PersonsController(mock.Object);

            persons.ToList().Remove(new Person()
            {
                FirstName = "test1", Id = 1
            });

            // Act

            personsController.Delete(1);
            IEnumerable <Person> result = personsController.Get();

            //Assert

            Assert.Equal(persons, result);
        }
        public void GetById_test()
        {
            // Arrange

            IEnumerable <Person> persons = new Person[] {
                new Person()
                {
                    FirstName = "test1", Id = 1, Age = 2, Email = "*****@*****.**", LastName = "lastTest"
                },
                new Person()
                {
                    FirstName = "test2", Id = 2
                },
                new Person()
                {
                    FirstName = "test3", Id = 3
                },
                new Person()
                {
                    FirstName = "test4", Id = 4
                }
            }.ToList();
            Person expectedPerson = new Person()
            {
                FirstName = "test1", Id = 1, Age = 2, Email = "*****@*****.**", LastName = "lastTest"
            };

            Mock <IPersonsRepository> mock = new Mock <IPersonsRepository>();

            mock.Setup(r => r.GetById(It.IsAny <int>()))
            .Returns <int>((a) => persons.Where(y => y.Id == a).SingleOrDefault());

            PersonsController personsController = new PersonsController(mock.Object);

            // Act

            ObjectResult result = personsController.Get(1) as ObjectResult;
            Person       value  = result.Value as Person;

            //Assert

            Assert.Equal(expectedPerson.Id, value.Id);
        }
Ejemplo n.º 30
0
        public async Task PostTest()
        {
            var controller = new PersonsController(new PersonService());
            var newPerson  = new Person {
                FirstName = "Wayne",
                LastName  = "Rooney",
                Title     = "Footballer",
                Age       = 32,
                Address   = "Manchester",
                City      = "United",
                Phone     = "9875486",
                Email     = "*****@*****.**"
            };
            var result = await controller.Post(newPerson);

            var okResult = result.Should().BeOfType <CreatedAtActionResult>().Subject;
            var person   = okResult.Value.Should().BeAssignableTo <Person>().Subject;

            person.Id.Should().Be(1);
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="PersonsControllerTest"/> class.
 /// </summary>
 public PersonsControllerTest()
 {
     this.manager = new Mock<IPersonManager>();
     this.controller = new PersonsController(this.manager.Object);
     this.controller.Request = new HttpRequestMessage();
     this.controller.Configuration = new HttpConfiguration();
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="PersonsControllerTests"/> class.
 /// </summary>
 public PersonsControllerTests()
 {
     this.personsManager = new Mock<IPersonManager>();
     this.personsController = new PersonsController(this.personsManager.Object);
 }