2013-01-02 9 views
7

Tworzyłem aplikację Windows Store, ale mam problemy z wątkami testując metodę, która tworzy siatkę (która jest kontrolą XAML). Próbowałem przetestować przy użyciu NUnit i MSTest.Testowanie urządzenia Windows 8 Store Aplikacja UI (formanty Xaml)

Metoda badania jest:

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

A creator.CreateThumbnail (Metoda który wyrzuca błąd):

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

Kiedy uruchomić ten test generuje ten błąd:

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

Odpowiedz

9

Twój kod związany z sterowaniem musi być uruchomiony na wątku interfejsu użytkownika. Wypróbuj:

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

Powyższe powinno działać w teście MS. Nie wiem o NUnit.

+0

Dziękuję bardzo. Działa w teście MS. W NUnit to nie działa. –

Powiązane problemy