Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
402 views
in Technique[技术] by (71.8m points)

c# - Load JSON string to HttpRequestMessage

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 ScheduleRequest();
sr.Months = null;
sr.States = null;
sr.Zip = null;
sr.Miles = null;
sr.PCodes = null;
sr.PageStart = 1;
sr.PageLimit = 10;

HttpRequestMessage m = new HttpRequestMessage();
string sr_ = JsonConvert.SerializeObject(sr);

// How do I load it into the HttpRequestMessage???
// m.Content. = sr_;
var controller = new ShoppingCartController();

// Call the controlelr method and test if the return data is correct.
EventSyncResponse res = (EventSyncResponse)controller.CourseSchedule(m);

Am I doing this correctly, too?

Controller Code:

public object CourseSchedule(ScheduleRequest request)
{
    try
    {
        var result = cart.GetCourseSchedule(request);
        return Ok(result);
    } 
    catch (Exception ex)
    {
        if (ex.Message.StartsWith(@"ORA-20001"))
        {
            return Ok(new ParticipantResponse { FirstName = "No record found" });
        }
        throw ex;
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
[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
        //...
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...