public void Get_ShouldReturnPersonDataByID() { // Arrange //Call this method to seed the test data and to create a instance of Mock Object of In Memory Entity Setup(); input = 1; expected = 1; //Act //Calling Web Api HTTP Get Action Method by passing the ID var httpResult = personController.Get(input); //Converting the httpresult to Content Result var conNegResult = httpResult as NegotiatedContentResult <object>; //Casting the Content of Content Result to Target Object cp = (CompletePerson)conNegResult.Content; //Assert Assert.AreEqual(1, cp.PID); }
//This method is used to List out all the Person details if Id passed as 0 or no Paramater passed. //If its passed >0 then it will return the Singlt Person public ResponseResult PersonListDetail(int PID = 0) { ResponseResult r = new ResponseResult(); try { List <CompletePerson> cp = new List <CompletePerson>(); Person p = new Person(); if (PID == 0) { var pds = this.context.GetList(); foreach (PersonData item in pds) { //Converts the XML String to .Net objects p = HelperMethods.XMLStringToObject <Person>(item.PersonXML); cp.Add(new CompletePerson { PID = item.PID, PersomXML = p, CreatedDate = item.CreatedDate.ToString("MM/dd/yyyy"), UpdatedDate = item.UpdatedDate?.ToString("MM/dd/yyyy") }); } if (cp.Count == 0) { r.ReturnResult = cp; r.Message = "No record found"; r.status = ResponseStatus.Fail; } else { r.ReturnResult = cp; r.Message = "Retrived successfully."; r.status = ResponseStatus.Success; } } else { CompletePerson c = null; var pd = context.GetList().SingleOrDefault <PersonData>(req => req.PID == PID); if (pd != null) { //Converts the XML String to .Net objects p = HelperMethods.XMLStringToObject <Person>(pd.PersonXML); c = new CompletePerson { PID = pd.PID, PersomXML = p, CreatedDate = pd.CreatedDate.ToString("MM/dd/yyyy"), UpdatedDate = pd.UpdatedDate?.ToString("MM/dd/yyyy") }; } if (c == null) { r.ReturnResult = c; r.Message = "No record found"; r.status = ResponseStatus.Fail; LogFactory.Log(LogType.Error, LogMode.TextFile, "No record found."); } else { r.ReturnResult = c; r.Message = "Retrived successfully."; r.status = ResponseStatus.Success; } } } catch (Exception ex) { r.ReturnResult = null; r.Message = "Bad Request / Internal Server error."; r.status = ResponseStatus.Error; LogFactory.Log(LogType.Error, LogMode.TextFile, $"{ex.Message} \r\n {new StackTrace(ex, true).GetFrame(0).GetFileLineNumber()}"); } return(r); }