public void TopicsController_Post() { //a var config = new HttpConfiguration(); var request = new HttpRequestMessage(HttpMethod.Post, "http://localhost/api/v1/topics"); var route = config.Routes.MapHttpRoute("DefaultApi", "api/{content}/{id}"); var routeData = new HttpRouteData(route, new HttpRouteValueDictionary { {"controller", "topics"} }); _controller.ControllerContext = new HttpControllerContext(config, routeData, request); _controller.Request = request; _controller.Request.Properties[HttpPropertyKeys.HttpConfigurationKey] = config; var newTopic = new Topic() { Title = "test", Body = "testtest" }; //a var result = _controller.Post(newTopic); var json = result.Content.ReadAsStringAsync().Result; var topic = JsonConvert.DeserializeObject<Topic>(json); //a Assert.AreEqual(HttpStatusCode.Created, result.StatusCode); Assert.IsNotNull(topic); Assert.IsTrue(topic.Id > 0); Assert.IsTrue(topic.Created > DateTime.MinValue); }
public void TopicsControllerPost() { // Testing POST is harder than it should be but we need to do some work: var config = new HttpConfiguration(); var request = new HttpRequestMessage(HttpMethod.Post, "http://localhost/api/v1/topics"); var route = config.Routes.MapHttpRoute("DefaultApi", "api/{controller}/{id}"); var routeData = new HttpRouteData(route, new HttpRouteValueDictionary { { "controller", "topics" } }); _controller.ControllerContext = new HttpControllerContext(config, routeData, request); _controller.Request = request; _controller.Request.Properties[HttpPropertyKeys.HttpConfigurationKey] = config; var newTopic = new Topic() { Title = "Test title", Body = "Test body" }; var result = _controller.Post(newTopic); Assert.AreEqual(HttpStatusCode.Created, result.StatusCode); var json = result.Content.ReadAsStringAsync().Result; var topic = JsonConvert.DeserializeObject<Topic>(json); Assert.IsNotNull(topic); Assert.AreEqual("Test title", topic.Title); Assert.AreEqual("Test body", topic.Body); Assert.AreNotEqual(0, topic.Id); }
public bool AddTopic(Topic NewTopic) { try { _ctx.Topics.Add(NewTopic); return true; } catch (Exception e) { //TODO log this error return false; } }
public bool AddTopic(Topic topic) { try { _ctx.Topics.Add(topic); return true; } catch (Exception) { return false; } }
public bool AddTopic(Topic newTopic) { try { this.context.Topics.Add(newTopic); return true; } catch (Exception) { //TODO log this error return false; } }
public bool AddTopic(Topic newTopic) { try { _context.Topics.Add(newTopic); return true; } catch (Exception e) { //TODO logging return false; } }
public bool AddTopic(Topic newTopic) { try { _context.Topics.Add(newTopic); return true; } catch(Exception exception) { Console.WriteLine(exception); return false; } }
public void TopicsController_Post() { ConfigureWebApiContextForPost(_ctrl); var newTopic = new Topic() { Title = "I am the coolest test topic ever!", Body = "Check out this body... Is it the most epic topic body ever?? Smokin' hot!" }; var result = _ctrl.Post(newTopic); Assert.AreEqual(HttpStatusCode.Created, result.StatusCode); var json = result.Content.ReadAsStringAsync().Result; var topic = JsonConvert.DeserializeObject<Topic>(json); Assert.IsNotNull(topic); Assert.IsTrue(topic.Id > 0); Assert.IsTrue(topic.Created > DateTime.MinValue); }
public bool AddTopic(Topic topic) { topic.Id = new Random().Next(5, 1000); topic.Created = DateTime.UtcNow; return true; }
public bool AddTopic(Topic newTopic) { newTopic.Id = 4; return true; }