2013-01-02 12 views
7

Ho creato un'app di Windows Store ma ho problemi con i thread durante il test di un metodo che crea una griglia (che è un controllo XAML). Ho provato a testare usando NUnit e MSTest.Test unitario Interfaccia utente di Windows 8 Store App (Xaml Controls)

Il metodo di prova è:

[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); 
} 

E il creator.CreateThumbnail (Il metodo che genera l'errore):

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; 
} 

Quando ho eseguito questo test getta questo errore:

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

risposta

9

Il codice relativo ai controlli deve essere eseguito su un thread dell'interfaccia utente. Prova:

[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); 
} 

Quanto sopra dovrebbe funzionare su MS Test. Non so nulla di NUnit.

+0

Grazie mille. Funziona in MS Test. In NUnit non funziona. –

Problemi correlati