1
我想單元測試一個返回非原語的Web服務。根據請求,響應可以是xml或json。在我的測試中,如果我可以將內容主體反序列化爲我的一個對象,那將會很棒。下面是一些代碼:C#Wcf Web API反序列化響應
[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的,不知道怎麼辦。由於
太棒了,作品像一個魅力。我認爲我的代碼應該可以工作,但這是一個錯誤,將在未來版本中修復? – 2011-06-11 20:46:53
@nextgenneo正好。 – 2011-06-11 20:49:24
@NoahHeldman不在我的副本中,它沒有。它位於HttpContentExtensionMethods類中。 – 2011-09-28 01:14:48