public ActionResult Create(Person p) { if (ModelState.IsValid) { repo.Add(p); return RedirectToAction("Index"); } return View(); }
public int Add(Person p) { using (var db = new FamilyContext()) { db.People.Add(p); db.SaveChanges(); return p.Id; } }
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); }
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 ถึงจะ เขียวว ผ่าน }); }
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); } }
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 }); }
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); }
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); }
//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; }
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); } }
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); }
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); } }
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; } }
// constructor. public TheGenderProperty() { p = new Person(); }
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); }
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); }); }
public void MaleCannotMarryToMale() { Person p1 = new Person(); Person p2 = new Person(); p1.Gender = "M"; p2.Gender = "M"; Assert.Throws<Exception>(() => { p1.Marry(p2); }); }
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); }
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); }
public void MaleCanMarryToFemale() { Person p1 = new Person(); Person p2 = new Person(); p1.Gender = "M"; p2.Gender = "F"; Assert.DoesNotThrow(() => { p1.Marry(p2); }); }
public void ShouldReturn0YearOldIfBornInThisYear() { Person p = new Person(); int thisYear = DateTime.Now.Year; p.BirthYear = thisYear; int age = p.GetAge(); Assert.Equal(0, age); }
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); }); }
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); }); }
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; }
public ActionResult Edit(Person p) { if (ModelState.IsValid) { repo.Update(p); return RedirectToAction("Index"); } return View(p); }