Exemplo n.º 1
0
 public ActionResult Create(Person p)
 {
     if (ModelState.IsValid) {
     repo.Add(p);
     return RedirectToAction("Index");
       }
       return View();
 }
Exemplo n.º 2
0
 public int Add(Person p)
 {
     using (var db = new FamilyContext()) {
     db.People.Add(p);
     db.SaveChanges();
     return p.Id;
       }
 }
Exemplo n.º 3
0
        public void MarriedPersonHasFamily()
        {
            Person p1 = new Person() { Gender = "M" };
            Person p2 = new Person() { Gender = "F" };
            p1.Marry(p2);

            Assert.NotNull(p1.Family);
            Assert.NotNull(p2.Family);
            Assert.Same(p1.Family, p2.Family);
        }
Exemplo n.º 4
0
        public void BirthYeadIsNotInTherFutrue()
        {
            Person p = new Person(); // arrange เตรียม

            //Assert ถ้า รัน Code นี้จะโยน Exception
            Assert.Throws<Exception>(() =>
            {
                p.BirthYear = DateTime.Now.Year + 1;  //p.BirthYear = 3000;  // action (assume this year is 2012) เอาค่าไปใส่   //Asset ต้อง Throw Exception  ถึงจะ เขียวว ผ่าน
            });
        }
Exemplo n.º 5
0
 public void Delete(int id)
 {
     using (var db = new FamilyContext()) {
     Person p = new Person();
     p.Id = id;
     db.People.Attach(p);
     db.Entry(p).State = System.Data.EntityState.Deleted;
     int n = db.SaveChanges();
     Debug.Print("delete={0} rows", n);
       }
 }
Exemplo n.º 6
0
        public void BirthYearIsNotInTheFuture()
        {
            // arrange
              Person p = new Person();

              // assert
              Assert.Throws<Exception>(() => {
            // action (assume this year is 2012)
            p.BirthYear = DateTime.Now.Year + 1; // 3000
              });
        }
Exemplo n.º 7
0
        public void FamilyHasHusbandAndWife()
        {
            Person p1 = new Person() { Gender = "M" };

            Person p2 = new Person() { Gender = "F" };

            Family f = p1.Marry(p2);

            Assert.Same(f.Husband, p1);

            Assert.Same(f.Wife, p2);
        }
Exemplo n.º 8
0
        public void BirthYearInPastOrPresentIsOkay()
        {
            // arrange
              Person p = new Person();
              int thisYear = DateTime.Now.Year;

              // assert
              Assert.DoesNotThrow(() => {
            // action (assume this year is 2012)
            p.BirthYear = thisYear;
              });
              Assert.Equal(thisYear, p.BirthYear);
        }
Exemplo n.º 9
0
        //public void Marry(Person spouse)
        //{
        //    if (this.GetAge() < 20 || spouse.GetAge() < 20)
        //    {
        //        throw new Exception();
        //    }
        //    //return new Person();
        //}
        public Family Marry(Person spouse)
        {
            if (this.GetAge() < 20 || spouse.GetAge() < 20)
            {
                throw new Exception();
            }
            if (this.Gender == spouse.Gender)
            {
                throw new Exception();
            }
            Family f = new Family();

            return f;
        }
Exemplo n.º 10
0
        public void Update(Person p)
        {
            using (var db = new FamilyContext()) {
            var personFromDb = db.People.Find(p.Id); // state->unchanged

            // if (!p.Equals(personFromDb)) {
            personFromDb.Name = p.Name;
            personFromDb.Gender = p.Gender;
            personFromDb.BirthYear = p.BirthYear;
            // }

            int n = db.SaveChanges();
            Debug.Print("update={0} rows", n);
              }
        }
Exemplo n.º 11
0
 public void BirthYearInPastOrPresentIsOkay()
 {
     //arrange
     Person p = new Person();
     int thisYear = DateTime.Now.Year;
     //assert
     Assert.DoesNotThrow(() =>
     {
         //action (assume this year is 2012)
         //p.BirthYear = 3000;
         p.BirthYear = thisYear;
     });
     //check assert to check actual data (expect, acture)
     Assert.Equal(thisYear, p.BirthYear);
 }
Exemplo n.º 12
0
        public void Update(Person p)
        {
            using (var db = new FamilyContext())
            {
                var personFromDb = db.People.Find(p.Id);

                //if(!p.Equals(personFromDb))
               // {
                    //db.People.Attach(p);
                    //db.Entry(p).State = System.Data.EntityState.Modified;
                    personFromDb.Name = p.Name;
                    personFromDb.Gender = p.Gender;
                    personFromDb.BirthYear = p.BirthYear;
                //}

                //db.People.Attach(p);
                //db.Entry(p).State = System.Data.EntityState.Modified;
                int n = db.SaveChanges();
                Debug.Print("update={0} rows", n);
            }
        }
Exemplo n.º 13
0
 public Family Marry(Person spouse)
 {
     if (this.GetAge() < 20 || spouse.GetAge() < 20)
     {
         throw new Exception();
     }
     else if (this.Gender == spouse.Gender)
     {
         throw new Exception();
     }
     else
     {
         Family f = new Family();
         if (this.Gender.ToLower().Equals("m"))
         {
             f.Name = this.Name + spouse.Name;
             f.Husband = this;
             f.Wife = spouse;
         }
         else
         {
             f.Name = spouse.Name + this.Name;
             f.Husband = spouse;
             f.Wife = this;
         }
         this.Family = f;
         spouse.Family = f;
         return f;
     }
 }
Exemplo n.º 14
0
 // constructor.
 public TheGenderProperty()
 {
     p = new Person();
 }
Exemplo n.º 15
0
            public void ShouldReturnFamily()
            {
                Person p1 = new Person();
                Person p2 = new Person();

                p1.Gender = "M";
                p2.Gender = "F";

                Family f = p1.Marry(p2);

                Assert.NotNull(f);
            }
Exemplo n.º 16
0
            public void ThrowsExceptionIfAgeLessThan20Years()
            {
                Person p1 = new Person();
                Person p2 = new Person();
                int thisYear = DateTime.Now.Year;

                p1.BirthYear = thisYear - 15; // 15 yo.
                p2.BirthYear = thisYear - 30; // 30 yo.

                //Assert.Throws(typeof(Exception), () => {
                //  p1.Marry(p2);
                //});

                Assert.Throws<Exception>(() => {
                  p1.Marry(p2);
                });
            }
Exemplo n.º 17
0
            public void MaleCannotMarryToMale()
            {
                Person p1 = new Person();
                Person p2 = new Person();
                p1.Gender = "M";
                p2.Gender = "M";

                Assert.Throws<Exception>(() => {
                  p1.Marry(p2);
                });
            }
Exemplo n.º 18
0
            public void ShouldReturnValidAge()
            {
                Person p = new Person();
                int thisYear = DateTime.Now.Year;
                p.BirthYear = thisYear - 12;
                int age = 0;
                Assert.DoesNotThrow(() =>
                {
                    age = p.GetAge();

                });
                Assert.True(age >= 0);
            }
Exemplo n.º 19
0
            public void ShouldReturnValidAge()
            {
                Person p = new Person();
                int thisYear = DateTime.Now.Year;
                p.BirthYear = thisYear - 12;

                int age = p.GetAge();

                Assert.Equal(12, age);

                // Assert.True(age <= 12);
            }
Exemplo n.º 20
0
            public void MaleCanMarryToFemale()
            {
                Person p1 = new Person();
                Person p2 = new Person();
                p1.Gender = "M";
                p2.Gender = "F";

                Assert.DoesNotThrow(() => {
                  p1.Marry(p2);
                });
            }
Exemplo n.º 21
0
            public void ShouldReturn0YearOldIfBornInThisYear()
            {
                Person p = new Person();
                int thisYear = DateTime.Now.Year;
                p.BirthYear = thisYear;

                int age = p.GetAge();

                Assert.Equal(0, age);
            }
Exemplo n.º 22
0
            public void MaleCanMarryToFemaleIfAgeGreaterThan20()
            {
                Person p1 = new Person();
                Person p2 = new Person();

                int thisYear = DateTime.Now.Year;
                p1.BirthYear = thisYear - 30;     //20
                p2.BirthYear = thisYear - 30;     //20

                p1.Gender = "M";
                p2.Gender = "F";

                Assert.DoesNotThrow(() =>
                {
                    p1.Marry(p2);
                });
            }
Exemplo n.º 23
0
            public void ThrowExceptionIfAgeLessThan20Years()
            {
                Person p1 = new Person();
                Person p2 = new Person();
                int thisYear = DateTime.Now.Year;
                p1.BirthYear = thisYear;    //0
                p2.BirthYear = thisYear;    //0

                // Generics
                Assert.Throws<Exception>(() =>
                {
                    p1.Marry(p2);
                });
            }
Exemplo n.º 24
0
        public Family Marry(Person spouse)
        {
            if (this.GetAge() < 20 || spouse.GetAge() < 20) {
            throw new Exception();
              }
              if (this.Gender == spouse.Gender) {
            throw new Exception();
              }

              Family f = new Family();
              this.Family = f;
              spouse.Family = f;

              if (this.Gender == "M") {
            f.Husband = this;
            f.Wife = spouse;
              }
              else {
            f.Husband = spouse;
            f.Wife = this;
              }

              return f;
        }
Exemplo n.º 25
0
 public ActionResult Edit(Person p)
 {
     if (ModelState.IsValid)
     {
         repo.Update(p);
         return RedirectToAction("Index");
     }
     return View(p);
 }