I am trying to write unit tests to make sure that my bindings to view models are being appropriately set to controls within my XAML page.
I have a XAML page structured like this where the Page's DataContext is set to a ViewModel that's passed in when it is navigation to the page occurs (ellipses represent extra properties or controls that aren't related to my issue):
<Page x:Class="MyWin8Proj.Pages.SamplePage" ....>
<Page.Resources>....</Page.Resources>
<Page.TopAppBar>
<AppBar ...>
<customControl DataContext="{Binding}" .../>
</AppBar>
</Page.TopAppBar>
<Grid ...>
<ListView x:Name="SelectionList" ItemSource="{Binding ListPropertyInViewModel}"...>...</ListView>
<Grid DataContext="{Binding SelectedItem, ElementName=SelectionList}"...>
<TextBlock Text="{Binding SubPropertyOfItem}" /> ...
</Grid >
<Grid>
</Page>
In my unit test, I'm executing on the UI thread like I've seen mentioned in other articles/posts (like this one)
public IAsyncAction ExecuteOnUIThread(Windows.UI.Core.DispatchedHandler action)
{
return Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, action);
}
[TestMethod]
public async Task TestViewModelLoadedIntoContext()
{
await ExecuteOnUIThread(() =>
{
_targetPage = new MyWin8Proj.Pages.SamplePage();
_viewModel = new SampleViewModel();
_targetPage.DataContext= _viewModel;
});
}
When the test reaches the new MyWin8Proj.Pages.SamplePage()
line, it goes to the Page's constructor, which is simply:
public SamplePage()
{
this.InitializeComponent();
}
Here, I get thrown an error coming from the TopAppBar in my XAML -
WinRT information: Cannot deserialize XBF metadata property list as 'TopAppBar' was not found in type 'null'. [Line: 0 Position: 0]
Additional information: The text associated with this error code could not be found.
What does this mean?? Is there a way for me to unit test my View's bindings to ViewModels, or is this the wrong approach? Should I approach this an integration test with coded UI tests instead to make sure my bindings are correct?