예제 #1
0
        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());
            }
        }
        // by convention routing: POST/api.lawyers
        public HttpResponseMessage Post(Lawyer body)
        {
            using (LawyerService lawyerService = new LawyerService(new DataContext()))
            {
                if (!ModelState.IsValid)
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState));
                }

                lawyerService.Add(body);
                return(new HttpResponseMessage(HttpStatusCode.OK));
            }
        }