web-dev-qa-db-ja.com

JSON文字列をHttpRequestMessageに読み込む

WebAPI Webサービスのテストをいくつか書いていますが、テストでサービスメソッドにJSONを送信する方法がわかりません。

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);

私もこれを正しくやっていますか?

コントローラーコード:

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;
    }
}
13
MB34
[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
    }
}

しかし、私はあなたのアクションが実際にあなたのコントローラーでこのようにリファクタリングされるべきであるという印象を受けます

public class ShoppingCartController : ApiController {

    public IHttpActionResult CourseSchedule(ScheduleRequest model) { ... }

}

これは、孤立した単体テストをリファクタリングする必要があることを意味します...

[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
        //...
    }
}
22
Nkosi

MB34。メソッドにScheduleRequestパラメーターも追加する必要があります。このリンクを確認してください: http://www.lybecker.com/blog/2013/06/26/accessing-http-request-from-asp-net-web-api/

0
Samuel Grave