簡體   English   中英

測試基於任務的WCF調用

[英]Testing Task-based WCF call

我正在將在應用程序中進行的WCF調用轉換為異步運行,以確保GUI在獲取數據時能夠響應。 通常,我使用這些方法來填充ViewModel的屬性。

例如,這是我的新舊代碼:

private async Task LoadDataItems()
{
    //DataItems = Service.SelectDataItems();

    DataItems = await Service.SelectDataItemsAsync();
}

另外,這是一些使用RhinoMocks的測試代碼:

//Doesn't set DataItems when LoadDataItems() is called
myWcfServiceClient.Stub(async client => await client.SelectDataItemsAsync()).Return(new Task<List<DataItemDto>>(() => new List<DataItemDto> { testDataItem }));

//NullReferenceException on SelectDataItemsAsync()
myWcfServiceClient.Stub(client => client.SelectDataItemsAsync().Result).Return(new List<DataItemDto> { testDataItem });

基本上,在我的單元測試中,未設置DataItems或得到了NullReferenceException試圖偽造結果。 在RhinoMocks上,這可能比什么都重要。

在RhinoMocks中,您可以使用Task.FromResult(...)在基於任務的操作上定義Result Task.FromResult(...)

因此,我的測試代碼將如下設置結果:

myWcfServiceClient.Stub(client => client.SelectDataItemsAsync()).Return(Task.FromResult(new List<DataItemDto> { testDataItem }));

簡單,效果很好!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM