2013-03-07 21 views
0

我需要在Windows Phone中編寫單元測試以測試我的數據是否正在反序列化爲正確的類型。這是我迄今爲止所做的。如何測試Windows Phone中的異步方法

[TestMethod] 
    [Asynchronous] 
    public void SimpleTest() 
    { 
     await pots = help.potholes(); 

我收到一個錯誤,指出「pots」不能等待。 Pots是一個列表,應該接受來自正在對我的webservice進行異步調用的坑洞函數的結果。

這是使用Restharp進行調用的方法。

public void GetAllPotholes(Action<IRestResponse<List<Pothole>>> callback) 
    { 

     var request = new RestRequest(Configuration.GET_POTHOLE_ALL,Method.GET); 
     request.AddHeader("Accept", "application/json"); 
     _client.ExecuteAsync(request, callback); 

    } 

如何讓壺等候?什麼是在Windows Phone中測試休息服務的正確方法?

我使用的是Windows Phone的工具包測試框架

這是我下面的教程。 Asynchronous tests

回答

1

術語 「異步」 在.NET現在超載。

您正在參考的文章指的是awaitable方法,而不是通過回調異步的方法。

下面是你如何測試這個的粗略想法。

[TestMethod]   
[Asynchronous] 
public void SimpleTest() 
{ 
    // set up your system under test as appropriate - this is just a guess 
    var help = new HelpObject(); 

    help.GetAllPotholes(
     response => 
     { 
      // Do your asserts here. e.g. 
      Assert.IsTrue(response.Count == 1); 

      // Finally call this to tell the test framework that the test is now complete 
      EnqueueTestComplete(); 
     }); 
} 
0

您正在使用異步..等待在錯誤的道路

試試這個

public async void SimpleTest() 
{ 
    pots = await help.potholes(); 
    .... 
} 
1

亞光表示目前正在多個上下文中使用異步術語,在Windows Phone上的測試方法的情況下,你可以在你的代碼塊看是不是關鍵字,而是歸功於具有釋放工作線程以允許其他進程運行的目標,以及您的測試方法等待UI或服務請求中可能發生的任何更改。

你可以這樣做,讓你的測試等待。

[TestClass] 
public class ModuleTests : WorkItemTest 
{ 
    [TestMethod, Asynchronous] 
    public void SimpleTest() 
    { 
     var pots; 
     EnqueueDelay(TimeSpan.FromSeconds(.2)); // To pause the test execution for a moment. 
     EnqueueCallback(() => pots = help.potholes()); 
     // Enqueue other functionality and your Assert logic 
     EnqueueTestComplete(); 
    } 
} 
相關問題