public void TestPostTopic() { ExpectedCourses expectedCourses = new ExpectedCourses(); string courseName = expectedCourses.GetCourses()[0].title; DbTopics dbTopics = new DbTopics(); string topicName = "Test Topic"; ObjectResult result = (ObjectResult)topicsController.Post(courseName, topicName); int? statusCode = result.StatusCode; Assert.Equal(201, statusCode); Assert.True(dbTopics.IsExistTopic(courseName, topicName)); topicsController.Delete(courseName, topicName); }
public void TopicsController_Post() { // 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" } }); _ctrl.ControllerContext = new HttpControllerContext(config, routeData, request); _ctrl.Request = request; _ctrl.Request.Properties[HttpPropertyKeys.HttpConfigurationKey] = config; var newTopic = new Topic() { Title = "Test Topic", Body = "This is a test topic" }; 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 void TopicControllerPostTest() { var area = CreateAreaContext(); var Logic = CreateLogic(); var Controller = new TopicsController(Logic); var Topic = new Topic { Id = Guid.NewGuid(), Name = "First Topic", Area = area, AreaId = area.Id }; var result = Controller.Post(TopicDTO.ToModel(Topic)); var createdResult = result as CreatedAtRouteResult; var model = createdResult.Value as TopicDTO; Assert.AreEqual(Topic.Name, model.Name); }