Example #1
0
        public void TestCreateMethod_CreateFourPeoples()
        {
            // Arrange
            People inputPeople1 = new People();

            inputPeople1.FirstName   = "Harry";
            inputPeople1.LastName    = "Porter";
            inputPeople1.Address     = Constant.Constant.Address;
            inputPeople1.Gender      = Constant.Constant.Gender;
            inputPeople1.Interests   = Constant.Constant.Interests;
            inputPeople1.DateOfBirth = DateTime.Now;

            People inputPeople2 = new People();

            inputPeople2.FirstName   = "Harriet";
            inputPeople2.LastName    = "Huang";
            inputPeople2.Address     = Constant.Constant.Address;
            inputPeople2.Gender      = Constant.Constant.Gender;
            inputPeople2.Interests   = Constant.Constant.Interests;
            inputPeople2.DateOfBirth = DateTime.Now;


            People inputPeople3 = new People();

            inputPeople3.FirstName   = "Hunter";
            inputPeople3.LastName    = "Yin";
            inputPeople3.Address     = Constant.Constant.Address;
            inputPeople3.Gender      = Constant.Constant.Gender;
            inputPeople3.Interests   = Constant.Constant.Interests;
            inputPeople3.DateOfBirth = DateTime.Now;

            People inputPeople4 = new People();

            inputPeople4.FirstName   = "Peter";
            inputPeople4.LastName    = "P.";
            inputPeople4.Address     = Constant.Constant.Address;
            inputPeople4.Gender      = Constant.Constant.Gender;
            inputPeople4.Interests   = Constant.Constant.Interests;
            inputPeople4.DateOfBirth = DateTime.Now;


            var mockSet = new Mock <DbSet <People> >();

            var mockContext = new Mock <PeopleContext>();

            mockContext.Setup(m => m.Peoples).Returns(mockSet.Object);


            // Act
            var service = new PeopleService(mockContext.Object);

            service.Create(inputPeople1);
            service.Create(inputPeople2);
            service.Create(inputPeople3);
            service.Create(inputPeople4);

            // Assert
            mockSet.Verify(m => m.Add(It.IsAny <People>()), Times.AtLeast(4));
            mockContext.Verify(m => m.SaveChanges(), Times.AtLeast(4));
        }
        public IActionResult Create(PeopleViewModel person) //personViewModel uses to avoid users to access Id
        {
            if (ModelState.IsValid)                         //Alltid kontrollera input på backend
            {
                _peopleService.Create(person.Name, person.Country);

                return(RedirectToAction("Index"));
            }
            return(View(person)); //personViewModel uses to sent wrong input to View
        }
Example #3
0
        public void TestCreateMethod_PeopleWithoutImage()
        {
            // Arrange
            People inputPeople = new People();

            inputPeople.FirstName   = Constant.Constant.Firstname;
            inputPeople.LastName    = Constant.Constant.LastName;
            inputPeople.Address     = Constant.Constant.Address;
            inputPeople.Gender      = Constant.Constant.Gender;
            inputPeople.Interests   = Constant.Constant.Interests;
            inputPeople.DateOfBirth = DateTime.Now;

            var mockSet     = new Mock <DbSet <People> >();
            var mockContext = new Mock <PeopleContext>();

            mockContext.Setup(m => m.Peoples).Returns(mockSet.Object);

            // Act
            var service = new PeopleService(mockContext.Object);

            service.Create(inputPeople);

            // Assert
            mockSet.Verify(m => m.Add(It.IsAny <People>()), Times.Once());
            mockContext.Verify(m => m.SaveChanges(), Times.Once());
        }
Example #4
0
        public void TestCreateMethod_PeopleWithEmptyInformation()
        {
            // Arrange
            People inputPeople = new People();

            inputPeople.FirstName   = string.Empty;
            inputPeople.LastName    = string.Empty;
            inputPeople.Address     = string.Empty;
            inputPeople.Gender      = string.Empty;
            inputPeople.Interests   = string.Empty;
            inputPeople.DateOfBirth = DateTime.Now;
            inputPeople.Image       = null;

            var mockSet     = new Mock <DbSet <People> >();
            var mockContext = new Mock <PeopleContext>();

            mockContext.Setup(m => m.Peoples).Returns(mockSet.Object);

            // Act
            var service = new PeopleService(mockContext.Object);

            service.Create(inputPeople);

            // Assert
            mockSet.Verify(m => m.Add(It.IsAny <People>()), Times.Never());
            mockContext.Verify(m => m.SaveChanges(), Times.Never());
        }
Example #5
0
        public void TestCreateMethod_PeopleWithImage()
        {
            // Arrange
            People inputPeople = new People();

            inputPeople.FirstName   = Constant.Constant.Firstname;
            inputPeople.LastName    = Constant.Constant.LastName;
            inputPeople.Address     = Constant.Constant.Address;
            inputPeople.Gender      = Constant.Constant.Gender;
            inputPeople.Interests   = Constant.Constant.Interests;
            inputPeople.DateOfBirth = DateTime.Now;


            Image        img = System.Drawing.Image.FromFile("../../Content/Images/HarryPotter.jpg");
            MemoryStream ms  = new MemoryStream();

            img.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
            inputPeople.Image = ms.ToArray();

            var mockSet     = new Mock <DbSet <People> >();
            var mockContext = new Mock <PeopleContext>();

            mockContext.Setup(m => m.Peoples).Returns(mockSet.Object);

            // Act
            var service = new PeopleService(mockContext.Object);

            service.Create(inputPeople);

            // Assert
            mockSet.Verify(m => m.Add(It.IsAny <People>()), Times.Once());
            mockContext.Verify(m => m.SaveChanges(), Times.Once());
        }
Example #6
0
        public ActionResult <Users> Create(Peoples people)
        {
            var peopleDb = GetPerCpf(people.Cpf.ToString());

            if (peopleDb.Value != null && people.Cpf == peopleDb.Value.Cpf)
            {
                return(BadRequest("Error: Já existe uma pessoa com esse cpf"));
            }

            var cepValid = VerifyCep(people);

            if (cepValid)
            {
                switch (people.Sexo)
                {
                case Sexo.Masculino:
                    people.Sexo = Sexo.Masculino;
                    break;

                case Sexo.Feminino:
                    people.Sexo = Sexo.Feminino;
                    break;
                }
                _peopleService.Create(people);
                return(CreatedAtRoute("GetPeople", new { id = people.Id }, people));
            }

            return(BadRequest("CEP não existe em nosso banco de dados."));
        }
Example #7
0
 public IActionResult Create(People people)
 {
     if (ModelState.IsValid)
     {
         peopleService.Create(people);
         return(RedirectToAction(nameof(Index)));
     }
     return(View(people));
 }
Example #8
0
        public void TestCreateMethod_PeopleWithNullObject()
        {
            // Arrange
            People inputPeople = null;
            var    mockSet     = new Mock <DbSet <People> >();
            var    mockContext = new Mock <PeopleContext>();

            mockContext.Setup(m => m.Peoples).Returns(mockSet.Object);

            // Act
            var service = new PeopleService(mockContext.Object);

            service.Create(inputPeople);

            // Assert
            mockSet.Verify(m => m.Add(It.IsAny <People>()), Times.Never());
            mockContext.Verify(m => m.SaveChanges(), Times.Never());
        }
Example #9
0
 public ActionResult Create(People people)
 {
     peopleService.Create(people);
     return(RedirectToAction("Index"));
 }
Example #10
0
 public async Task<IActionResult> OnPostAsync()
 {
     _service.Create(Person);
     return RedirectToPage("./ListPeople");
 }
Example #11
0
 private static void Add()
 {
     var person = new Person();
     Edit(person);
     PeopleService.Create(person);
 }