Exemplo n.º 1
0
        public void SaveCandidate_EnsureWorks_And_DataPersists()
        {
            Candidate candidate = new Candidate
            {
                FirstName   = "Eduardo",
                LastName_1  = "Perez",
                Email       = "*****@*****.**",
                PhoneNumber = "(442) 123-4567"
            };

            var controller = new CandidatesController();

            controller.Request       = new HttpRequestMessage();
            controller.Configuration = new HttpConfiguration();

            //insert
            var result = controller.SaveCandidateData(candidate);

            Assert.AreEqual(HttpStatusCode.Created, result.StatusCode);
            Candidate c;

            Assert.IsTrue(result.TryGetContentValue <Candidate>(out c));
            Assert.IsNotNull(c.id);
            Assert.AreEqual("Eduardo", c.FirstName);
            _inserted_id = c.id;

            //update
            candidate.id         = _inserted_id;
            candidate.LastName_2 = "updated lastname_2";
            result = controller.SaveCandidateData(candidate, candidate.id);
            Assert.AreEqual(HttpStatusCode.OK, result.StatusCode);
            c = null;
            Assert.IsTrue(result.TryGetContentValue <Candidate>(out c));
            Assert.IsNotNull(c.id);
            Assert.AreEqual(candidate.LastName_2, c.LastName_2);

            result = controller.GetCandidateData(_inserted_id);

            Assert.IsNotNull(result);
            Assert.AreEqual(HttpStatusCode.OK, result.StatusCode);
            c = null;
            Assert.IsTrue(result.TryGetContentValue <Candidate>(out c));
            Assert.AreEqual("Eduardo", c.FirstName);

            //luego buscar algo que no existe
            result = controller.GetCandidateData("3");
            Assert.IsNotNull(result);
            Assert.AreEqual(HttpStatusCode.NotFound, result.StatusCode);

            //y por ultimo el borrado de lo creado
            result = controller.DeleteCandidate(_inserted_id);
            Assert.IsNotNull(result);
            Assert.AreEqual(HttpStatusCode.OK, result.StatusCode);

            //y  borrado de lo que no existe
            result = controller.DeleteCandidate("3");
            Assert.IsNotNull(result);
            Assert.AreEqual(HttpStatusCode.NotFound, result.StatusCode);
        }