I am trying to unit test a controller method create
I want to test if the data is saved to the database, I have created a Model class called Person
where I have attributes such as person name, last name, email and age
public class Person
{
[Key]
public int PersonID { get; set; }
[Display(Name = "Name")]
[Required]
public string name { get; set; }
[Display(Name = "Last Name")]
[Required]
public string lastName { get; set; }
[Display(Name = "Email")]
[Required]
public string email { get; set; }
[Display(Name = "Age")]
[Required]
public int age { get; set; }
}
Then I have my controller method Create
that I want to test whether it is saving on the database or not.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "PersonID,name,lastName,email,age")] Person person)
{
if (ModelState.IsValid)
{
db.MyPersons.Add(person);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(person);
}