public void CanEditFamily() { // Arrange AdminController target = new AdminController(GetPersonRepository(), GetFamilyRepository()); // Act Family f1 = target.EditFamily(1).ViewData.Model as Family; Family f2 = target.EditFamily(2).ViewData.Model as Family; Family f3 = target.EditFamily(3).ViewData.Model as Family; // Assertion Assert.AreEqual(1, f1.Id); Assert.AreEqual(2, f2.Id); Assert.AreEqual(3, f3.Id); }
public void IndexContainsAllFamilies() { // Arrange AdminController target = new AdminController(GetPersonRepository(), GetFamilyRepository()); // Act Family[] result = ((IEnumerable<Family>)target.Index().ViewData.Model).ToArray(); // Assertion Assert.AreEqual(4, result.Length); Assert.AreEqual("Doe", result[0].FamilyName); Assert.AreEqual("Deer", result[1].FamilyName); Assert.AreEqual("Doey", result[2].FamilyName); Assert.AreEqual("Deerly", result[3].FamilyName); }
public void CannotEditInvalidFamily() { // Arrange AdminController target = new AdminController(GetPersonRepository(), GetFamilyRepository()); // Act Family result = (Family)target.EditFamily(5).ViewData.Model; // Assertion Assert.IsNull(result); }
public void CanDeletePersonId() { // Arrange // need extra Moq functionality so can't use the normal mock repo Person person = new Person{Id=2, FamilyId=1, ParentId=0, ParentId2=0, FirstName="Jane", LastName="Doe"}; Mock < IPersonRepository > mock = new Mock<IPersonRepository>(); mock.Setup(m => m.People).Returns(new List<Person> { new Person{Id=1, FamilyId=1, ParentId=0, ParentId2=0, FirstName="John", LastName="Doe"}, person, new Person{Id=3, FamilyId=1, ParentId=1, ParentId2=2, FirstName="Jennifer", LastName="Deer"} }); AdminController target = new AdminController(mock.Object, GetFamilyRepository()); // Act target.DeletePerson(person.Id); // Assertion // assert that the delete functionality was called with the correct ID mock.Verify(m => m.DeletePerson(person.Id)); }
public void CanDeleteFamilyId() { // Arrange // need extra Moq functionality so can't use the normal mock repo Family family = new Family{Id=2, FamilyName="Deer", Description="The Deer Family"}; Mock<IFamilyRepository> mock = new Mock<IFamilyRepository>(); mock.Setup(m => m.Families).Returns(new List<Family> { new Family{Id=1, FamilyName="Doe", Description="The Doe Family"}, family, new Family{Id=3, FamilyName="Doey", Description="The Doey Family"} }); AdminController target = new AdminController(GetPersonRepository(), mock.Object); // Act target.DeleteFamily(family.Id); // Assertion // assert that the delete functionality was called with the correct ID mock.Verify(m => m.DeleteFamily(family.Id)); }
public void CannotSaveInvalidPersonChanges() { // Arrange // need extra Moq functionality so can't use the normal mock repo, but this isn't dependant on content anyway Mock<IPersonRepository> mock = new Mock<IPersonRepository>(); AdminController target = new AdminController(mock.Object, GetFamilyRepository()); Person person = new Person { FirstName = "Testable", LastName = "Test" }; // add a deliberate error to the model target.ModelState.AddModelError("error", "error"); // Act ActionResult result = target.EditPerson(person); // Assertion - save method was NOT called, and user WAS sent back to the family view mock.Verify(m => m.SavePerson(It.IsAny<Person>()), Times.Never()); Assert.IsInstanceOfType(result, typeof(ViewResult)); }
public void CanSaveValidPersonChanges() { // Arrange // need extra Moq functionality so can't use the normal mock repo, but this isn't dependant on content anyway Mock<IPersonRepository> mock = new Mock<IPersonRepository>(); AdminController target = new AdminController(mock.Object,GetFamilyRepository()); Person person = new Person { FirstName = "Testable", LastName = "Test" }; // Act ActionResult result = target.EditPerson(person); // Assertion - save method was called, and user was not sent back to the family view mock.Verify(m => m.SavePerson(person)); Assert.IsNotInstanceOfType(result, typeof(ViewResult)); }
public void CannotSaveInvalidFamilyChanges() { // Arrange // need extra Moq functionality so can't use the normal mock repo, but this isn't dependant on content anyway Mock<IFamilyRepository> mock = new Mock<IFamilyRepository>(); AdminController target = new AdminController(GetPersonRepository(), mock.Object); Family family = new Family { FamilyName = "Test", Description = "The Test Family" }; // add a deliberate error to the model target.ModelState.AddModelError("error", "error"); // Act ActionResult result = target.EditFamily(family); // Assertion - save method was NOT called, and user WAS sent back to the family view mock.Verify(m => m.SaveFamily(It.IsAny<Family>()), Times.Never()); Assert.IsInstanceOfType(result, typeof(ViewResult)); }
public void CanSaveValidFamilyChanges() { // Arrange // need extra Moq functionality so can't use the normal mock repo, but this isn't dependant on content anyway Mock<IFamilyRepository> mock = new Mock<IFamilyRepository>(); AdminController target = new AdminController(GetPersonRepository(), mock.Object); Family family = new Family { FamilyName = "Test", Description = "The Test Family" }; // Act ActionResult result = target.EditFamily(family); // Assertion - save method was called, and user was not sent back to the family view mock.Verify(m => m.SaveFamily(family)); Assert.IsNotInstanceOfType(result, typeof(ViewResult)); }