public void AddALawyer() { // setting up the expectations Lawyer newLawyer = new Lawyer { Name = "Neos", Surname = "Surneos", Initials = "NS", DateOfBirth = new DateTime(2000, 1, 1), Email = "*****@*****.**", GenderRefId = 1, TitleRefId = 1 }; CollectionAssert.DoesNotContain(Lawyers, newLawyer); using (LawyerService lawyerService = new LawyerService(MockContext.Object)) { // test add new lawyer lawyerService.Add(newLawyer); MockLawyersSet.Verify(m => m.Add(It.IsAny <Lawyer>()), Times.Once); CollectionAssert.Contains(Lawyers, newLawyer); Assert.AreEqual(true, newLawyer.Active); // also test that findByName fetches the new lawyer Assert.AreEqual(newLawyer, lawyerService.GetAll(Doubles.GetParameters(Name: newLawyer.Name)).FirstOrDefault()); // same goes with the surname Assert.AreEqual(newLawyer, lawyerService.GetAll(Doubles.GetParameters(Surname: newLawyer.Surname)).FirstOrDefault()); } }
public void GetOneLawyer() { // setting up the expectations var existing = Lawyers.ElementAt(1); var notExisting = new Lawyer { Id = 404 }; // test using (LawyerService lawyerService = new LawyerService(MockContext.Object)) { var actual = lawyerService.Get(existing.Id); MockLawyersSet.Verify(m => m.Find(It.IsAny <int>()), Times.Once); Assert.IsNotNull(actual); Assert.AreEqual(existing, actual); actual = lawyerService.Get(notExisting.Id); MockLawyersSet.Verify(m => m.Find(It.IsAny <int>()), Times.Exactly(2)); Assert.IsNull(actual); } }
public void UpdateALawyer() { // setting up the expectations // current lowyer is the one to be updated. Lawyer currentLawyer = Lawyers.ElementAt(1); // create the updated lawyer. This will become the new "Active" instace. Lawyer updatedLawyer = new Lawyer { Name = "Updated", Surname = "Surneos", Initials = "US", DateOfBirth = new DateTime(2000, 1, 1), Email = "*****@*****.**", GenderRefId = 1, TitleRefId = 1 }; // before any change we need to make sure the DB contains the current instance // and does not contain the updated (new) one. CollectionAssert.DoesNotContain(Lawyers, updatedLawyer); CollectionAssert.Contains(Lawyers, currentLawyer); using (LawyerService lawyerService = new LawyerService(MockContext.Object)) { // update the lawyer lawyerService.Update(currentLawyer.Id, updatedLawyer); // validate .Add is called only once MockLawyersSet.Verify(m => m.Add(It.IsAny <Lawyer>()), Times.Once); // make sure both instances are now in DB CollectionAssert.Contains(Lawyers, updatedLawyer); CollectionAssert.Contains(Lawyers, currentLawyer); // validate old instance is not Active Assert.AreEqual(false, currentLawyer.Active); // verify the new instance is Active Assert.AreEqual(true, updatedLawyer.Active); // we recall the new lawyer instace with his new surname Lawyer fetched2 = lawyerService.GetAll(Doubles.GetParameters(Surname: updatedLawyer.Surname)).FirstOrDefault(); Assert.AreEqual(updatedLawyer, fetched2); // verify that we cannot find the lawyer with his old surname Lawyer oldLawyer = lawyerService.GetAll(Doubles.GetParameters(Surname: currentLawyer.Surname)).FirstOrDefault(); Assert.IsNull(oldLawyer); // verify that we cannot update the inactive instace try { lawyerService.Update(currentLawyer.Id, new Lawyer()); } catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(HttpRequestValidationException)); Assert.AreEqual(string.Format("the lawyer record with id: '{0}' is archived.", currentLawyer.Id), ex.Message); } // verify we cannot update a non existing lawyer try { lawyerService.Update(404, new Lawyer()); } catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(NullReferenceException)); Assert.AreEqual("Lawyer not found.", ex.Message); } } }