2013-01-02 25 views
7

私はWindowsストアアプリケーションを作成していましたが、グリッド(XAMLコントロール)を作成するメソッドをテストするスレッドの問題があります。 NUnitとMSTestを使ってテストしようとしました。ユニットテストWindows 8ストアアプリケーションUI(Xamlコントロール)

試験方法は次のとおりです。

[TestMethod] 
public void CreateThumbnail_EmptyLayout_ReturnsEmptyGrid() 
{ 
    Layout l = new Layout(); 
    ThumbnailCreator creator = new ThumbnailCreator(); 
    Grid grid = creator.CreateThumbnail(l, 192, 120); 

    int count = grid.Children.Count; 
    Assert.AreEqual(count, 0); 
} 

とcreator.CreateThumbnail(エラーをスローするメソッド):私はこのテストを実行すると

public Grid CreateThumbnail(Layout l, double totalWidth, double totalHeight) 
{ 
    Grid newGrid = new Grid(); 
    newGrid.Width = totalWidth; 
    newGrid.Height = totalHeight; 

    SolidColorBrush backGroundBrush = new SolidColorBrush(BackgroundColor); 
    newGrid.Background = backGroundBrush; 

    newGrid.Tag = l;    
    return newGrid; 
} 

それは、このエラーがスローされます。

System.Exception: The application called an interface that was marshalled for a different thread. (Exception from HRESULT: 0x8001010E (RPC_E_WRONG_THREAD)) 

答えて

9

コントロール関連のコードは、UIスレッドで実行する必要があります。試してください:

[TestMethod] 
async public Task CreateThumbnail_EmptyLayout_ReturnsEmptyGrid() 
{ 
    int count = 0; 
    await ExecuteOnUIThread(() => 
    { 
     Layout l = new Layout(); 
     ThumbnailCreator creator = new ThumbnailCreator(); 
     Grid grid = creator.CreateThumbnail(l, 192, 120); 
     count = grid.Children.Count; 
    }); 

    Assert.AreEqual(count, 0); 
} 

public static IAsyncAction ExecuteOnUIThread(Windows.UI.Core.DispatchedHandler action) 
{ 
    return Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, action); 
} 

上記のMSテストで動作する必要があります。私はNUnitについて知らない。

+0

ありがとうございました。 MS Testで動作します。 NUnitでは動作しません。 –

関連する問題