public void Edit_GivenExistingPersonDto_ShouldUpdatePersonInDbContext() { //---------------Set up test pack------------------- var people = GetPeopleList(); var context = Substitute.For<ILendingLibraryContext>(); var repo = new PersonRepository(context); SetContextWithDtos(context, people); var existingPerson = people[0]; //---------------Assert Precondition---------------- //---------------Execute Test ---------------------- Assert.IsNotNull(existingPerson); existingPerson.Email = "*****@*****.**"; repo.Update(existingPerson); //---------------Test Result ----------------------- context.Received().SaveChanges(); var actual = context.People.FirstOrDefault(i => i.Id == 1); Assert.IsNotNull(actual); Assert.AreEqual(existingPerson.Email, actual.Email); }
public void Edit_GivenNonExistingPersonDto_ShouldAddPersonInDbContext() { //---------------Set up test pack------------------- var lwa = new PersonDto() { Id = 5, PersonName = "Lwando" }; var context = Substitute.For<ILendingLibraryContext>(); var repo = new PersonRepository(context); SetContextWithDtos(context, new List<PersonDto>()); //---------------Assert Precondition---------------- //---------------Execute Test ---------------------- repo.Update(lwa); var addedPerson = repo.GetPersonById(lwa.Id); //---------------Test Result ----------------------- context.Received().SaveChanges(); Assert.IsNotNull(addedPerson); Assert.AreEqual(lwa, addedPerson); }
public void GetPersonById_GivenPersonIdDoesNotExist_ShouldReturnNull() { //---------------Set up test pack------------------- var people = GetPeopleList(); var context = Substitute.For<ILendingLibraryContext>(); var repo = new PersonRepository(context); SetContextWithDtos(context, people); //---------------Assert Precondition---------------- //---------------Execute Test ---------------------- var result = repo.GetPersonById(5); //---------------Test Result ----------------------- Assert.IsNull(result); }
public void GetPersonById_GivenPersonId_ShouldReturnPersonDto() { //---------------Set up test pack------------------- var people = GetPeopleList(); var context = Substitute.For<ILendingLibraryContext>(); var repo = new PersonRepository(context); SetContextWithDtos(context, people); //---------------Assert Precondition---------------- //---------------Execute Test ---------------------- var actual = repo.GetPersonById(1); //---------------Test Result ----------------------- Assert.IsNotNull(actual); Assert.IsInstanceOf(typeof(PersonDto), actual); Assert.AreEqual(1, actual.Id); }
public void Delete_GivenPersonIdDoesNotExist_ShouldNotCallSaveAllChangesAndRemove() { //---------------Set up test pack------------------- var people = GetPeopleList(); var context = Substitute.For<ILendingLibraryContext>(); var repo = new PersonRepository(context); SetContextWithDtos(context, people); //---------------Assert Precondition---------------- //---------------Execute Test ---------------------- repo.Delete(5); context.DidNotReceive().SaveChanges(); context.People.DidNotReceive().Remove(Arg.Any<PersonDto>()); //---------------Test Result ----------------------- }
public void Delete_GivenPersonId_ShouldCallSaveAllChanges() { //---------------Set up test pack------------------- var people = GetPeopleList(); var context = Substitute.For<ILendingLibraryContext>(); var repo = new PersonRepository(context); SetContextWithDtos(context, people); //---------------Assert Precondition---------------- //---------------Execute Test ---------------------- repo.Delete(1); context.Received().SaveChanges(); //---------------Test Result ----------------------- }
public void Delete_GivenPersonId_ShouldDeleteThatPerson() { //---------------Set up test pack------------------- var people = GetPeopleList(); var context = Substitute.For<ILendingLibraryContext>(); var repo = new PersonRepository(context); SetContextWithDtos(context, people); //---------------Assert Precondition---------------- //---------------Execute Test ---------------------- repo.Delete(1); var result = context.People.Where(p => p.Id == 1); //---------------Test Result ----------------------- Assert.AreEqual(0, result.Count()); }