1

我正在尝试对返回非原语的 Web 服务进行单元测试。根据请求,响应可以是 xml 或 json。在我的测试中,如果我可以将内容主体反序列化为我的对象之一,那就太好了。这是一些代码:

    [WebGet(UriTemplate = "{arg1}/{arg2}")]
    public HttpResponseMessage<MyType> GetSomethingCool(string arg1, long arg2)
    {    
         return new HttpResonseMessage<MyTpe>(new MyType());
    }

    public class MyType
    {
         public string Property1 { get; set; }
         public long Property2 { get; set; }
         public string Property3 { get; set; }
    }

我的测试:

    [Test]
    public void MyTestForTheWebService_ReturnsText()
    {
          Service1 service = new  Service1 (_mockRepository.Object);
          HttpResponseMessage<MyType> result = service.GetSomethingCool("Something", **);

          Assert.AreEqual(HttpStatusCode.OK, result.StatusCode);

          MyType resultContent = result.Content.ReadAs<MyType>(); // doesn't work
          Assert.AreEqual("expected value", resultContent .Property1);
          .....

所以基本上我需要能够将 result.Content 转换为 MyType 并且不知道如何。谢谢

4

1 回答 1

1

尝试做:

MyType resultContent = (MyType)result.Content.ReadAs();

我相信你遇到了一个已知的错误。

于 2011-06-11T20:27:07.990 回答