Load JSON string to HttpRequestMessage

后端 未结 2 2075
独厮守ぢ
独厮守ぢ 2020-12-17 10:07

I\'m writing some tests for my WebAPI web service and cannot figure out how to send JSON to my service method in the test.

ScheduleRequest sr = new ScheduleR         


        
相关标签:
2条回答
  • 2020-12-17 10:17
    [TestClass]
    public class ShoppingCartControllerTests {
        [TestMethod]
        public void TestCourseSchedule() {
            //Arrange
            var sr = new ScheduleRequest();
            sr.Months = null;
            sr.States = null;
            sr.Zip = null;
            sr.Miles = null;
            sr.PCodes = null;
            sr.PageStart = 1;
            sr.PageLimit = 10;
    
            var json = JsonConvert.SerializeObject(sr);
            //construct content to send
            var content = new System.Net.Http.StringContent(json, Encoding.UTF8, "application/json");
            var request = new HttpRequestMessage {
                RequestUri = new Uri("http://localhost/api/shoppingcart"),
                Content = content
            };
    
            var controller = new ShoppingCartController();
            //Set a fake request. If your controller creates responses you will need this
            controller.Request = request;
            //Act
            // Call the controller method and test if the return data is correct.
            var response = controller.CourseSchedule(request) as OkNegotiatedContentResult<List<EventSyn‌​cResponse>> ;
    
            //Assert
            //...other asserts
        }
    }
    

    But I get the impression that your Action should actually be refactored like this in your controller

    public class ShoppingCartController : ApiController {
    
        public IHttpActionResult CourseSchedule(ScheduleRequest model) { ... }
    
    }
    

    which would mean that your isolated unit test should be refactored to...

    [TestClass]
    public class ShoppingCartControllerTests {
        [TestMethod]
        public void TestCourseSchedule() {
            //Arrange
            var sr = new ScheduleRequest();
            sr.Months = null;
            sr.States = null;
            sr.Zip = null;
            sr.Miles = null;
            sr.PCodes = null;
            sr.PageStart = 1;
            sr.PageLimit = 10;
    
           var controller = new ShoppingCartController();
            //Set a fake request. If your controller creates responses you will need this
            controller.Request = new HttpRequestMessage {
                RequestUri = new Uri("http://localhost/api/shoppingcart"),
            };
            //Act
            // Call the controller method and test if the return data is correct.
            var response = controller.CourseSchedule(sr) as OkNegotiatedContentResult<List<EventSyn‌​cResponse>> ;;
    
            //Assert
            //...
        }
    }
    
    0 讨论(0)
  • 2020-12-17 10:29

    MB34. You need to add in your method, a ScheduleRequest parameter too. Check this link: http://www.lybecker.com/blog/2013/06/26/accessing-http-request-from-asp-net-web-api/

    0 讨论(0)
提交回复
热议问题