3
我的WebAPI實現與方法是這樣的:的WebAPI在內存測試 - 收到錯誤序列化的IQueryable
public IEnumerable<Device> GetAllDevices()
public Device GetDeviceById(int id)
看起來不錯,但在IIS或selfhosted運行時的工作原理。正確返回JSON對象。
但是,在我嘗試使用內存服務器的單元測試中,第一種方法失敗。
System.InvalidOperationException : Cannot create and populate list type System.Linq.IQueryable`1[Test.Device].
這回歸到Newtonsoft.Json.Serialization程序集。測試示例如下:
[Test]
public void GET_AskingForListOfDevices_GettingOk200WithListOfDevicesInJSON()
{
var client = new HttpClient(InMemoryServer);
HttpRequestMessage request = CreateRequest("api/devices", "application/json", HttpMethod.Get);
using (HttpResponseMessage response = client.SendAsync(request).Result)
{
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
Assert.NotNull(response.Content);
Assert.AreEqual("application/json", response.Content.Headers.ContentType.MediaType);
// next line throws exc
var content = response.Content.ReadAsAsync<IQueryable<Device>>().Result;
Assert.AreEqual(3, content.Count());
}
request.Dispose();
}
任何想法在哪裏看?
UPDATE
下面的例子會引發該錯誤,但我發現解決方案來避免它。只需使用IList <>而不是IQueryable <>。仍然沒有回答我爲什麼它在提琴手中工作的問題。它使用相同的技巧嗎?
你可以顯示這些行動方法和你的測試的實施。 – tugberk